【发布时间】:2017-06-05 06:43:43
【问题描述】:
我有一个收藏看起来像
{
"Aid":12234,
"items":{
"itemId":"SP897474",
"Blocks":[
{
"blockId":"W23456",
"name":"B1",
"innerBlock":[
{
"id":"S23490",
"name":"IB1",
"state":true
},
{
"id":"S23491",
"name":"IB2",
"state":true
},
{
"id":"S23492",
"name":"IB3",
"state":true
}
],
"active":true
},
{
"blockId":"W23457",
"name":"B2",
"innerBlock":[
{
"id":"S23482",
"name":"IB1",
"state":true
},
{
"id":"S23483",
"name":"IB2",
"state":true
}
],
"active":false
}
]
},
"active":true
}
由于嵌套,我无法更新 innerBlock 数组的字段,特别是名称、状态。基本上,我想更新提到的字段。我已经尝试过这个查询
User.update({
"items.Blocks.innerBlock.id": req.body.id,
"Aid": req.body.Aid
}, {
"$set": {
"items.Blocks.$.InnerBlock.$.name": req.body.name
}
})
它显示下面给出的错误
"errmsg": "Too many positional (i.e. '$') elements found in path 'items.Blocks.$.InnerBlock.$.name'"
我不确定如何解决它。mongo shell 中的查询可能是什么?谢谢。
【问题讨论】:
-
你试过items.Blocks.InnerBlock.name
-
是的,我试过了。它显示像这样的错误
"errmsg": "cannot use the part (innerBlock of "items.Blocks.innerBlock.name) to traverse the element -
看看这个答案,我认为它可以帮助update nester array with mongoose
-
试试这个
"items.Blocks[0].InnerBlock[0].name": req.body.name -
我在这里找到了解决方案jasonmciver.com/mongo-query-deep-array
标签: arrays node.js mongodb mongoose