Mongodb多次更新,仅更新mongo-shell中的单个文档

时间:2015-05-27 10:59:51

标签: mongodb mongo-shell

我正在学习mongodb并尝试使用{$ multi:true}选项执行简单的多文档更新,但它只更新单个文档。

> 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

请帮助我真的卡住..

1 个答案:

答案 0 :(得分:5)

不是$multi,只能写{multi:true}

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

请参阅此处的文档,以获取有关upsert或writeconcern params的更多帮助 http://docs.mongodb.org/manual/reference/method/db.collection.update/

相关问题