I am completely new in mongodb. I have a collection with these reocrds in it.
MongoDB Enterprise > db.Users.find()
{ "_id" : ObjectId("618ac9c1b2bd7211de8dea73"), "username" : "user1", "favorites" : { "countries" : [ "Germany", "Tibet" ], "language" : [ "english", "germany", "italian" ] } }
{ "_id" : ObjectId("618aca52b2bd7211de8dea74"), "username" : "user2", "favorites" : { "language" : [ "english", "germany" ] } }
{ "_id" : ObjectId("618aca78b2bd7211de8dea75"), "username" : "user3" }
suppose I want to add a value for all records with follwoing condition
db.Users.update({"favorites.language":"english"},{$addToSet:{"favorites.language":"french"}})
but it just updates the record for user1. but user2 has also {"favorites.language":"english"} . But it does not update. can any one help me for that?
CodePudding user response:
You need to add multi:true to be able to update all records as follow:
db.Users.update({"favorites.language":"english"},{$addToSet:{"favorites.language":"french"}},{multi:true})