我有以下表示项目的JSON结构
{
Id: "a",
Array1: [{
Id: "b",
Array2: [{
Id: "c",
Array3: [
{...}
]
}]
}]
}
我需要能够用新项替换 Array2
中的数组元素,或者用新数组替换 Array3
.
这是我在 Array2
中替换数组项的代码:
await Collection.UpdateOneAsync(
item => item.Id.Equals("a") &&
item.Array1.Any(a => a.Id.Equals("b")) &&
item.Array1[-1].Array2.Any(b => b.Id.Equals("c")),
Builders<Item>.Update.Set(s => s.Array1[-1].Array2[-1], newArray2Item)
);
执行此代码时,我收到此错误:
"A write operation resulted in an error.
Too many positional (i.e. '$') elements found in path 'Array1.$.Array2.$'"
这是我在 Array2
中替换 Array3
的代码:
await Collection.UpdateOneAsync(
item => item.Id.Equals("a") &&
item.Array1.Any(a => a.Id.Equals("b")) &&
item.Array1[-1].Array2.Any(b => b.Id.Equals("c")),
Builders<Item>.Update.Set(s => s.Array1[-1].Array2[-1].Array3, newArray3)
);
这是错误:
"A write operation resulted in an error.
Too many positional (i.e. '$') elements found in path 'Array1.$.Array2.$.Array3'"
我正在使用C#MongoDB驱动程序版本2.5.0和MongoDB版本3.6.1
我发现这个Jira机票Positional Operator Matching Nested Arrays表示问题已修复,他们建议使用此语法进行更新
Update all matching documents in nested array:
db.coll.update({}, {$set: {“a.$[i].c.$[j].d”: 2}}, {arrayFilters: [{“i.b”: 0}, {“j.d”: 0}]})
Input: {a: [{b: 0, c: [{d: 0}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}
Output: {a: [{b: 0, c: [{d: 2}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}
所以我将它转换为我的元素:
db.getCollection('Items').update(
{"Id": "a"},
{$set: {"Array1.$[i].Array2.$[j].Array3": [newArray3]}},
{arrayFilters:
[
{"i.Id": "b"},
{"j.Id": "c"}
]}
)
并得到这个错误:
cannot use the part (Array1 of Array.$[i].Array2.$[j].Array3) to traverse the element
有关如何解决此错误的任何想法?
1 回答
这是你需要的C#版本: