3 回答

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
不幸的是,$每個(gè)鍵不能多次使用運(yùn)算符,因此其余部分必須使用數(shù)字值。如:
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對更新嵌套數(shù)組的支持很差。因此,如果您需要經(jīng)常更新數(shù)據(jù),則最好避免使用它們,并考慮改用多個(gè)集合。
一種可能:創(chuàng)建forecasts自己的集合,并假設(shè)您具有一組固定的level值,則創(chuàng)建level一個(gè)對象而不是一個(gè)數(shù)組:
{
_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}]
},
]
}
}
}
然后,您可以使用以下命令進(jìn)行更新:
db.myCollection.update({
_id: 123,
'levels.proven.configs.config': 'Custom 1'
},
{ $set: { 'levels.proven.configs.$': newData }}
)
- 3 回答
- 0 關(guān)注
- 2085 瀏覽
添加回答
舉報(bào)