0

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

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

除非我_id从 newdocument 中删除该字段,否则什么都不做,即del newdocument["_id"]. 这是预期的行为吗?

4

1 回答 1

1

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

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

将工作

于 2013-01-30T19:45:08.380 回答