Update nested array element in mongodb

守給你的承諾、 提交于 2020-11-29 19:15:09

问题


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.


回答1:


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.

Query (Shell):

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
  }
);


来源:https://stackoverflow.com/questions/58953960/update-nested-array-element-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!