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 ...並且沒有聚合框架!那些日子...... –