I am working with MongoDB in one of my project. For the next feature I need to add a flag to array of objects by looping with a matching condition.
Following is an example of document that I have -
{
"_id" : ObjectId("5aaa4eb211a1c1c1f74c1657"),
"colors" : [
{ "status" : "done" },
{ "status" : "done" },
{ "status" : "n/a" }
]
}
Output should be -
{
"_id" : ObjectId("5aaa4eb211a1c1c1f74c1657"),
"colors" : [
{
"status" : "done",
"is_in_production" : true
},
{
"status" : "done",
"is_in_production" : true
},
{
"status" : "n/a"
}
]
}
I used the following query -
db.getCollection('products').update(
{
"colors.status": {
"$in":[
"done"
]
}
},
{$set: { 'colors.$[].is_in_production' :true }},
{multi: true}
)
That resulted the following error -
cannot use the part(colors of colors.$[].is_in_production)totraversetheelement({ colors: [{ "status" : "done" }, { "status" : "done" }, { "status" : "n/a" }] )}
Can someone help me with the query?? Thanks in Advance