Have a db.users collection, with documents similar to the example below:
{
"_id" : "4cff4345d95d8fc58cd34fe83325c8e",
"name": "bob",
"meals" : [
{
"fruits" : {
"favourite" : "apple",
"amount" : 5
},
"name" : "breakfast",
},
{
"fruits" : {
"amount" : 3
},
"name" : "lunch",
}
],
"age": 30
}
Wish to remove the favourite attribute from each entry in the db.users collection where meals.name is equal to breakfast
So running the needed query above against the entire db.users collection would result in a modification such that for the entry above, it becomes:
{
"_id" : "4cff4345d95d8fc58cd34fe83325c8e",
"name": "bob",
"meals" : [
{
"fruits" : {
"amount" : 5
},
"name" : "breakfast",
},
{
"fruits" : {
"amount" : 3
},
"name" : "lunch",
}
],
"age": 30
}
Note that the "favourite" : "apple", is now removed from the first sub-array element because the query matched the meal.name as equaling breakfast.
If anyone willing to assist, of course am happy to field any questions / clarify further if unclear.
Justification for asking this question - There is another SOF question here that is asking something that on face value appears similar. However, it was focused on a select
query type of operation, and I could not determine an answer to my question based on the question / answer offered there. Here, I wish to perform a mutation
(update / removal of attribute).
Further update - the answer given in comment resolved this for me - and is unrelated / sufficiently different to the answers in the aforementioned SOF answers. Believe this question / answer is a reasonable / useful addition to SOF and should not be marked as a duplicate based on the other question alone.