1
我刚开始学习mongo,并且认为{}
是指集合中的所有文档,并且正在考虑这个查询:db.foo.update({}, { $set: { letter : 'a' } }, { multi : true });
将更新集合中的所有文档。简单的mongo查询
以防万一,我使用MongoDB shell version: 2.0.4
> for (i=0; i<3; i++) db.foo.insert({ num : i });
> db.foo.find()
{ "_id" : ObjectId("510debe5ccc97edd4aca03dc"), "num" : 0 }
{ "_id" : ObjectId("510debe5ccc97edd4aca03dd"), "num" : 1 }
{ "_id" : ObjectId("510debe5ccc97edd4aca03de"), "num" : 2 }
> db.foo.update({}, { $set: { letter : 'a' } }, { multi : true });
> db.foo.find()
{ "_id" : ObjectId("510debe5ccc97edd4aca03dd"), "num" : 1 }
{ "_id" : ObjectId("510debe5ccc97edd4aca03de"), "num" : 2 }
{ "_id" : ObjectId("510debe5ccc97edd4aca03dc"), "letter" : "a", "num" : 0 }
非常感谢!这很有帮助 – Vor
@Vor:假设你没有使用2.2,shell的界面发生了变化:[“在2.2之前,在mongo shell中,upsert和multi是位置布尔选项”](http:// docs.mongodb.org/manual/reference/method/db.collection.update/#db.collection.update) –
@ muistooshort +1 ...并且没有聚合框架!那些日子...... –