updateMany with different values MongoDB

Viewed 148

I have a collection in MongoDB called "Users". Here, I want to update two specefic documents with different values on request. So far, I have succeded with selecting them both, but I can't seem to find a way to specefy the values. I can only give them the same value. Here is what I have so far:

db.db(DBName).collection("Users").updateMany(

{id: {$in: [1, 2]}}, 

{$set: {Value: "test"}}

This works fine but both of them of cause gains the value "test". Is there a way to specefy what the id: 1 gains the value "test1" and the id: 2 gains the value "test2"?

1 Answers

Update many is similar to a SQL query that matches multiple documents in an UPDATE statement. It's meant to update many documents with the same new data. If you would like to set different records to different values, simply issue .update() twice (or more) with your different conditions and different update values.

Related