4

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..

ArchNoob
  • 3,946
  • 5
  • 32
  • 59
  • problem is that your documents contains only one documents which contains `title` field and having value `blackers` , and your update query update same field value if you changed `blackers to blackers1` it update only one documents, if you want to set title write as `update({'color': 'black'}, {$set: {'title': 'blackers122'}},true,true)` – Neo-coder May 27 '15 at 11:08

1 Answers1

9

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/

bagrat
  • 7,158
  • 6
  • 29
  • 47
Nishant
  • 3,614
  • 1
  • 20
  • 26
  • hhaha.. thank you very much, I had that error in upsert and multi and couldn't turn my eye right for hours.. Cheers.. – ArchNoob May 27 '15 at 11:12