MongoDb更改了嵌入式doc中的值

时间:2015-03-09 09:04:58

标签: mongodb

{ 
"_id" : ObjectId("54fd4ddaa037ba481d794f5e"), 
"question" : "let me go?", 
"choices" : [ 
    { 
        "text" : "yes", 
        "_id" : ObjectId("54fd4ddaa037ba481d794f60"), 
        "votes" : [ ] 
    }, { 
        "text" : "y", 
        "_id" : ObjectId("54fd4ddaa037ba481d794f5f"), 
        "votes" : [ ] 
    } 
], 
"__v" : 0 
}

我想将值“y”更改为“no”,怎么样? 祝你的回答 感谢

1 个答案:

答案 0 :(得分:1)

在更新中使用 $set 运算符和 $ positional 运算符来设置数组中的元素,而无需显式指定元素的位置在数组即

db.collection.update(
    { "question": "let me go?", "choices.text": "y" }, 
    { $set: { "choices.$.text": "no" } }
);