我在下面有一个集合。我正在尝试更新数组元素。
我正在尝试更新 lineItem _id 值是否为 1,然后转到规范列表并将特性值从 900 更新为 50,如果 specName 为“Model”,如您所见,_id 也是一个数组。
采集数据:
{
"_id": "100",
"name": "Campaign",
"status": "Active",
"parts": {
"lineItem": [
{
"_id": [
{
"name": "A",
"value": "1"
}
],
"spec": [
{
"specName": "Brand",
"characteristicsValue": [
{
"value": "500"
}
]
},
{
"specName": "Model",
"characteristicsValue": [
{
"value": "900"
}
]
}
]
},
{
"_id": [
{
"name": "B",
"value": "2"
}
],
"spec": [
{
"specName": "Brand",
"characteristicsValue": [
{
"value": "300"
}
]
},
{
"specName": "Model",
"characteristicsValue": [
{
"value": "150"
}
]
}
]
},
{
"_id": [
{
"name": "C",
"value": "2"
}
]
}
]
}
}
相关更新没有按我的预期工作。
db.Collection.update({"parts.lineItem._id.value" : "1",
"parts.lineItem.spec.specName" : "Model" },{ $set: {
"parts.lineItem.spec.$.characteristicsValue" : "50" } })
编辑:每个 _id 都有一个规范数组。所以,我们需要找到_id,然后到_id数组下的spec,找到品牌并更新值。