Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - updateMany with different values MongoDB

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"?

question from:https://stackoverflow.com/questions/65847518/updatemany-with-different-values-mongodb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...