【问题标题】:Mongodb update object in multi nested array多嵌套数组中的MongoDB更新对象
【发布时间】: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'

我不明白我在这里做错了什么。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    我能够找出问题所在。出于某种原因,mongodb 不喜欢 another_element 中的。将其重命名为另一个元素有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2015-01-31
      • 2023-01-12
      相关资源
      最近更新 更多