2013-02-09 51 views
0

檢索到的原始數據有存儲在mongodb一些數據和檢索到的變量與studentstudent = coll.find_one({"name":"Cammy"})龍捲風 - 更換從MongoDB的

{ "_id" : ObjectId("511367bebb8027582a953cce"), "name" : "Cammy", "desc" : "does well in Math" }

我想改變和student一些屬性執行:student['desc'] = "does well in Physics"。爲了替換原始文件,我使用了coll.save(student)。但不是替換,而是出現了與namedesc相同的新記錄,但出現了不同的_id。 我應該如何替換原始文檔?

回答

1
use 

db.coll.findAndModify({ 
     query: { name: "cami"}, 
     update: { $set: { desc: 'does well in physics' } } 
}); 
+0

你能投票答案 – user1447121 2013-02-25 23:55:34