【发布时间】:2020-11-01 19:37:36
【问题描述】:
我正在尝试更新 Mongodb 中多嵌套数组内的对象。我知道我不能将位置 $ 运算符用于多嵌套数组。所以我尝试改用arrayFilters。
这是我的查询结构:
var objTest = {
name: 'blah',
color: 'blablah'
}
SomeModel.updateOne(
{
an_id: anId,
"an_array.another_array.and_another_array.some_id": id,
},
{
$set: {
"an_array.$[element].another_array.$[another_element].and_another_array": objTest,
},
},
{
arrayFilters: [
{ "element.name": 'test' },
{ "another_element.id": 'test2' },
],
}
)
.then((result) => {
console.log("result ", result);
resolve(result);
})
.catch((err) => {
console.log("err is ", err);
reject(err);
});
因此,按照示例,我正在尝试更新与 some_id 匹配的对象 and_another_array 数组。当我尝试这个时,我收到了错误The top-level field name must be an alphanumeric string beginning with a lowercase letter, found 'another_element'
我不明白我在这里做错了什么。
【问题讨论】: