更新 mongodb 中的嵌套数组元素 [英] Update nested array element in mongodb

查看:82
本文介绍了更新 mongodb 中的嵌套数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我而言,我有一个带有嵌套数组的 mongodb 文档,我需要更新属性数组元素.我的mongodb文档如下.

In my case I have mongodb document with nested arrays and I need to update attributes array elements. My mongodb document as follows.

{
"_id" : ObjectId("5dca9c5ece5b91119746eece"),
"updatedAt" : ISODate("2019-11-20T11:48:55.339Z"),
"attributeSet" : [ 
    {
        "attributeSetName" : "test-0",
        "type" : "Product",
        "id" : "1574158032603",
        "updatedAt" : ISODate("2019-11-19T10:10:53.783Z"),
        "createdAt" : ISODate("2019-11-19T10:07:20.084Z"),
        "attributes" : [ 
            {
                "attributeName" : "test-attribute",
                "defaultValue" : 123,
                "isRequired" : false,
                "id" : "1574221129398"
            }, 
            {
                "attributeName" : "test-attribute-02",
                "defaultValue" : 456,
                "isRequired" : false,
                "id" : "1574250533840"
            }
        ]
    }, 
    {
        "attributeSetName" : "test-1",
        "type" : "Product",
        "id" : "1574158116355",
        "updatedAt" : ISODate("2019-11-19T10:08:37.251Z"),
        "createdAt" : ISODate("2019-11-19T10:08:37.251Z"),
        "attributes" : []
    }
  ]
}

我需要更新属性数组中的元素并将更新文档放入结果对象中.这是我到目前为止所尝试的.

I need to update element in attributes array and get the updates document into result object. This is what I tried so far.

const result = await this.model.findOneAndUpdate(
{
    _id: settingsToBeUpdated._id,
    "attributeSet": {
        $elemMatch: {
            "attributeSet.id": attributeSetId,
            "attributes": {
                $elemMatch: {
                    'attributes.id': id
                }
            }
        }
    }
},
{
    $set: {
        'attributeSet.$[outer].attributes.$[inner].attributeName': attributeDto.attributeName,
        'attributeSet.$[outer].attributes.$[inner].defaultValue': attributeDto.defaultValue,
        'attributeSet.$[outer].attributes.$[inner].isRequired': attributeDto.isRequired,
    }
},
{
    "arrayFilters": [
        { "outer.id": attributeSetId },
        { "inner.id": id }
    ]
}
);

它不会更新模型.我参考了这个链接,但没有帮助.任何建议将不胜感激.

It does not update the model. I refered to this link, but it does not help. Any suggestion would be appreciated.

推荐答案

查询需要几处整改,否则就差不多了.更新不起作用,因为 attributeSet(文档数组)字段的 $elemMatch 将发生在 id 属性上在那些要过滤的文档中,而不是在 attributeSet.id 上,它不知道它是什么.并且不需要嵌套的 elemMatch,只需使用点表示法.

Couple of rectification required on the query, otherwise its almost there. The update is not working because $elemMatch for attributeSet (array of docs) field is to happen on id property of those docs to filter and not on attributeSet.id, it woudn't figure what it is. And nested elemMatch is not required, simply use dot notation.

要调试,您可以使用查找查询进行尝试.

To debug you can try it out with a find query.

查询(外壳):

db.collection.findOneAndUpdate(
  {
    _id: settingsToBeUpdated._id,
    attributeSet: {
      $elemMatch: {
        id: attributeSetId,
        "attributes.id": id
      }
    }
  },
  {
    $set: {
      "attributeSet.$[as].attributes.$[a].attributeName":
        attributeDto.attributeName,
      "attributeSet.$[as].attributes.$[a].defaultValue":
        attributeDto.defaultValue,
      "attributeSet.$[as].attributes.$[a].isRequired": attributeDto.isRequired
    }
  },
  {
    arrayFilters: [{ "as.id": attributeSetId }, { "a.id": id }],
    returnNewDocument: true
  }
);

这篇关于更新 mongodb 中的嵌套数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆