为什么我必须在更新之前删除Mongo中的_id字段?

时间:2013-01-30 18:06:34

标签: mongodb pymongo

我的更新找到了一个文档,更改了一些字段和更新:

newdocument = db.collection.findOne{"id_" : ObjectId("2bfc42346cb2f36c4f3fc6264c")}
newdocument.somefield = "New value" 
db.collection.update({"_id" : ObjectId("2bfc42346cb2f36c4f3fc6264c")}, newdocument)

除非我从newdocument中移除_id字段,即del newdocument["_id"],否则不执行任何操作。这是预期的行为吗?

1 个答案:

答案 0 :(得分:1)

id是不可变的,但你写的结构也不需要使用。简单地:

db.collection.update({"_id" : ObjectId("2bfc42346cb2f36c4f3fc6264c")}, {$set:{"somefield":"New value"}})

将起作用