3 回答

TA貢獻1752條經驗 獲得超4個贊
不幸的是,$每個鍵不能多次使用運算符,因此其余部分必須使用數字值。如:
db.myCollection.update({
"id": 1,
"forecasts.forecast-id": 123,
"forecasts.levels.level": "proven",
"forecasts.levels.configs.config": "Custom 1"
},
{"$set": {"forecasts.$.levels.0.configs.0": newData}}
)
MongoDB對更新嵌套數組的支持很差。因此,如果您需要經常更新數據,則最好避免使用它們,并考慮改用多個集合。
一種可能:創建forecasts自己的集合,并假設您具有一組固定的level值,則創建level一個對象而不是一個數組:
{
_id: 123,
parentId: 1,
name: "Forecast 1",
levels: {
proven: {
configs: [
{
config: "Custom 1",
variables: [{ x: 1, y:2, z:3}]
},
{
config: "Custom 2",
variables: [{ x: 10, y:20, z:30}]
},
]
},
likely: {
configs: [
{
config: "Custom 1",
variables: [{ x: 1, y:2, z:3}]
},
{
config: "Custom 2",
variables: [{ x: 10, y:20, z:30}]
},
]
}
}
}
然后,您可以使用以下命令進行更新:
db.myCollection.update({
_id: 123,
'levels.proven.configs.config': 'Custom 1'
},
{ $set: { 'levels.proven.configs.$': newData }}
)
- 3 回答
- 0 關注
- 2089 瀏覽
添加回答
舉報