慕瓜9086354
您需要使用两个概念:MongoDB位置算子并简单地为要更新的条目使用数字索引。位置运算符允许您使用如下条件:{"heros.nickname": "test"}然后引用已找到的数组条目,如下所示:{"heros.$ // db.denis.insert({_id:"43434", heros : [{ nickname : "test", items : ["", "", ""] }, { nickname : "test2", items : ["", "", ""] }]});
> db.denis.update(
{"heros.nickname": "test"},
{$set: {
"heros.$.items.1": "new_value"
}})> db.denis.find(){
"_id" : "43434",
"heros" : [
{"nickname" : "test", "items" : ["", "new_value", "" ]},
{"nickname" : "test2", "items" : ["", "", "" ]}
]}