我的更新找到了一个文档,更改了一些字段和更新:
newdocument = db.collection.findOne{"id_" : ObjectId("2bfc42346cb2f36c4f3fc6264c")}
newdocument.somefield = "New value"
db.collection.update({"_id" : ObjectId("2bfc42346cb2f36c4f3fc6264c")}, newdocument)
除非我从newdocument中移除_id
字段,即del newdocument["_id"]
,否则不执行任何操作。这是预期的行为吗?
答案 0 :(得分:1)
id是不可变的,但你写的结构也不需要使用。简单地:
db.collection.update({"_id" : ObjectId("2bfc42346cb2f36c4f3fc6264c")}, {$set:{"somefield":"New value"}})
将起作用