MongoDB中精确元素数组中的更新字段我有一个这样的文档:{
_id:"43434",
heroes : [
{ nickname : "test", items : ["", "", ""] },
{ nickname : "test2", items : ["", "", ""] },
]}我能$set类的第二个元素。items数组中嵌入对象的数组。heros带着nickname "test" ?结果:{
_id:"43434",
heroes : [
{ nickname : "test", items : ["", "new_value", ""] }, // modified here
{ nickname : "test2", items : ["", "", ""] },
]}
3 回答
白猪掌柜的
TA贡献1893条经验 获得超10个赞
{"heros.nickname": "test"}{"heros.$ // <- the dollar represents the first matching array key index> 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" : ["", "", "" ]}
]}
杨魅力
TA贡献1811条经验 获得超6个赞
db.collection.update({heroes:{$elemMatch:{ "nickname" : "test"}}},
{
$push: {
'heroes.$.items': {
$each: ["new_value" ],
$position: 1
}
}
})- 3 回答
- 0 关注
- 3256 浏览
添加回答
举报
0/150
提交
取消
