I want to update an array of strings which is nested inside an array of Objects.
This is my Mongo document:
{
"_id" : ObjectId("5a52d995734d1d388d17eb0b"),
"teams" : [
{
"assignedModels" : [
"5a1665a82c7eb90001a7d676",
"58d8fc2d734d1d5af6dd4803"
]
},
{
"assignedModels" : [
"58d8fc2d734d1d5af6dd4803"
]
}
]
}
Now i want to remove "58d8fc2d734d1d5af6dd4803" string from the assignedModels of each team object.
I have already tried some queries, but nothing is working as i expected
Current query:
db.collection('organisations').updateMany(
{ _id: database.ObjectID("5a52d995734d1d388d17eb0b") ,'teams.assignedModels' : { $exists:true } },
{ $pull : {'teams.$.assignedModels' : "58d8fc2d734d1d5af6dd4803" } },
{ multi: true });
Current output:
updated the 0th element of the teams array correctly but does not traverse through the other objects.
i tried $[], teams.assignedModels as well
Expected output:
{
"_id" : ObjectId("5a52d995734d1d388d17eb0b"),
"teams" : [
{
"assignedModels" : [
"5a1665a82c7eb90001a7d676"
]
},
{
"assignedModels" : [
]
}
]
}