如何更新嵌套数组中的多个文档?(MongoDB 3.6)
我正在尝试更新多个深度嵌套数组中的所有如何更新嵌套数组中的多个文档?(MongoDB 3.6),mongodb,Mongodb,我正在尝试更新多个深度嵌套数组中的所有customerId道具: // Collection before update [{ cycle: [ [[{customerId: "1"}], [], [], [{customerId: "1"}], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [{customerId: "1"}], [], [], [
customerId
道具:
// Collection before update
[{
cycle: [
[[{customerId: "1"}], [], [], [{customerId: "1"}], [], [], []],
[[], [], [], [], [], [], []],
[[], [], [], [], [], [], []],
[[], [{customerId: "1"}], [], [], [], [], []],
]
}]
// Collection after update
[{
cycle: [
[[{customerId: "2"}], [], [], [{customerId: "2"}], [], [], []],
[[], [], [], [], [], [], []],
[[], [], [], [], [], [], []],
[[], [{customerId: "2"}], [], [], [], [], []],
]
}]
我正在运行MongoDB 3.6。我尝试了$
、$[]
、$[]
和阵列过滤器的各种组合:
db.arrayTest.update({},
{ "$set": { "cycle.$[].$[day].customerId": "2"}},
{ arrayFilters: [{"day.customerId": "1"}] } )
// WriteResult({
// "nMatched" : 0,
// "nUpserted" : 0,
// "nModified" : 0,
// "writeError" : {
// "code" : 28,
// "errmsg" : "Cannot create field 'customerId' in element {0: [ { customerId: \"1\" } ]}"
// }
// })
db.arrayTest.update(
{"cycle.$[].$[].customerId": "1"},
{ "$set": { "cycle.$[].$[].customerId": "2"}}
)
// WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
db.arrayTest.update(
{"cycle.$.$.customerId": "1"},
{ "$set": { "cycle.$.$.customerId": "2"}}
)
// WriteResult({
// "nMatched" : 0,
// "nUpserted" : 0,
// "nModified" : 0,
// "writeError" : {
// "code" : 2,
// "errmsg" : "Too many positional (i.e. '$') elements found in path 'cycle.$.$.customerId'"
// }
// })
我还尝试了各种查询,但没有成功:
db.arrayTest.find({"cycle.$.$.customerId": "1"})
// ""
db.arrayTest.find({"cycle.$[].$[].customerId": "1"})
// ""
// The following works but requires using explicit array indices
db.arrayTest.find({"cycle.0.0.customerId": "1"})
// { "_id": ObjectId("5bdbeae940eedc517cafb84f"), "cycle": [
// [
// [{ "customerId": "1" }],
// [],
// [],
// [{ "customerId": "1" }],
// [],
// [],
// []
// ],
// [
// [],
// [],
// [],
// [],
// [],
// [],
// []
// ],
// [
// [],
// [],
// [],
// [],
// [],
// [],
// []
// ],
// [
// [],
// [{ "customerId": "1" }],
// [],
// [],
// [],
// [],
// []
// ]
// ] }
以下查询工作正常:
db.arrayTest.find({"cycle": {$elemMatch: { $elemMatch: { $elemMatch: { "customerId": "1"}}}}})
对于MongoDB,3.6+可以很好地实现这一点。MongoDB 3.6版本还添加了$[]
和$[]
。例如:
db.arrayTest.insertMany([{
array: [
[[{id: "1"}], [{id: "2"}], [], [{id: "1"}], []]
]
}])
db.arrayTest.update({},
{ $set: { "array.$[].$[].$[el].id": "updated" } },
{ arrayFilters: [{"el.id": "1"}], multi: true}
)
db.arrayTest.find({})
// [{
// array: [
// [[{id: "updated"}], [{id: "2"], [], [{id: "updated"}], []]
// ]
// }]
你能不能也包括你尝试过的东西,以及你遇到的错误是什么?@Andreas Done。谢谢