0

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....

asked Apr 22, 2022 at 4:47

1 Answer 1

2

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

answered Apr 22, 2022 at 5:01
Sign up to request clarification or add additional context in comments.

1 Comment

@SugumarVenkatesan I update my answer with an official reference

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.