用于更新的MongoDB查询
find({user:“saturngod”}) 回归用于更新的MongoDB查询,mongodb,Mongodb,find({user:“saturngod”}) 回归 { "_id" : ObjectId("4de20ef97065cc77c80541fd"), "todo" : [ { "id" : 1, "desc" : "hi", "done" : 0 }, { "id" : 2, "desc" : "hello", "done" : 0 } ], "user" : "s
{ "_id" : ObjectId("4de20ef97065cc77c80541fd"),
"todo" : [
{
"id" : 1,
"desc" : "hi",
"done" : 0
},
{
"id" : 2,
"desc" : "hello",
"done" : 0
}
], "user" : "saturngod" }
我想在todo.id=1时更新done=1
所以,我写了
>db.tasks.update({'todo.id':1},{"$set":{todo:{done:1}}});
我丢了所有要做的事情,只做了一件事:1
db.tasks.find()
如何更新值?我想这样做
{ "_id" : ObjectId("4de20ef97065cc77c80541fd"),
"todo" : [
{
"id" : 1,
"desc" : "hi",
"done" : 1
},
{
"id" : 2,
"desc" : "hello",
"done" : 0
}
], "user" : "saturngod" }
明白了
db.tasks.update({'todo.id':1},{"$set":{"todo.$.done":1}});
明白了
db.tasks.update({'todo.id':1},{"$set":{"todo.$.done":1}});