Home > front end >  Update inner array in multiple array document
Update inner array in multiple array document

Time:04-22

I am trying this question... I don't want use the "_id" : "12", field..

{
    "_id" : ObjectId("62622dd73905f04f59db2971"),
    "array1" : [ 
        {
            "_id" : "12",
            "array2" : [ 
                {
                    "_id" : "123",
                    "answeredBy" : []
                }, 
                {
                    "_id" : "124",
                    "answeredBy" : []
                }
            ]
        }
    ]
}

I am trying to update using the following query

db.getCollection('nestedArray').updateMany(
 {'array1.array2._id':'123'},
 {$push:{'array1.array2.$[inner].answeredBy':'success'}},
 {arrayFilters:[{'inner._id':'123'}]}
)

But I am getting the following error:

"errmsg" : "The path 'array1.array2' must exist in the document in order to apply array updates.",

I just trying to understand what is wrong with the code....

CodePudding user response:

add $[] between array1 and array2

Update Nested Arrays in Conjunction with $[]

db.collection.update({
  "array1.array2._id": "123"
},
{
  $push: {
    "array1.$[].array2.$[inner].answeredBy": "success"
  }
},
{
  arrayFilters: [
    {
      "inner._id": "123"
    }
  ]
})

mongoplayground

  • Related