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
Sugumar Venkatesan
4,04410 gold badges49 silver badges83 bronze badges
1 Answer 1
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"
}
]
})
answered Apr 22, 2022 at 5:01
YuTing
6,6692 gold badges8 silver badges19 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
YuTing
@SugumarVenkatesan I update my answer with an official reference
lang-js