Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb multi update, only updates a single document in mongo-shell

I am learning mongodb and am trying to perform a simple multi documents update with the {$multi: true} option, but it only updates a single document.

> db.users.update({'color': 'black'}, {$set: {'title': 'blackers'}}, {$multi: true});

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.users.find({'color': 'black'});

{ "_id" : ObjectId("5562312f061fa19e6cebc7e4"), "color" : "black", "hobby" : [ "dancing", "breaking", "drawing", "praying", "smiling" ], "name" : "john", "schools" : [ "mlimwa", "martin", "ignatus", "eget", "jubilee", "canon" ], "title" : "blackers" }

{ "_id" : ObjectId("5564057bb0f557f0c0589e66"), "color" : [ "black" ], "name" : "demo" }

{ "_id" : ObjectId("556424fc1dd78ab02dd2918f"), "color" : "black", "name" : "tola" }

>db.version()
3.0.3

Please help am really stuck..

like image 216
ArchNoob Avatar asked Feb 11 '23 00:02

ArchNoob


1 Answers

It is not $multi, write only {multi:true}

db.users.update({'color': 'black'}, {$set: {'title': 'blackers'}}, {multi: true});

refer the documentation here, for further help, regarding upsert, or writeconcern params http://docs.mongodb.org/manual/reference/method/db.collection.update/

like image 104
Nishant Avatar answered Feb 12 '23 14:02

Nishant