1

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
sAntd
  • 135
  • 1
  • 2
  • 15
  • Please update the question with actual sample documents (not images) and desired o/p structure to have better visibility on context and get help. – ambianBeing Nov 20 '19 at 12:08

1 Answers1

2

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
  }
);
ambianBeing
  • 3,449
  • 2
  • 14
  • 25