1

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.

arcseldon
  • 35,523
  • 17
  • 121
  • 125
  • Can you simply build your new item and replace it? If the logic is complex, build a new object and replace it is a better solution. – Herbert Yu Jun 07 '17 at 23:24
  • No, that is not a readily viable solution - in reality, the "collection" is actually vastly more complex, and contains subIds and so on, referenced elsewhere. Here I am just looking for the "pattern" to achieve above with mutation either for a single item keyed on ID, or ideally for a full scan of the collection in a single step. – arcseldon Jun 07 '17 at 23:31
  • @HerbertYu - thanks for the suggestion all the same. Do you know of any possibilities mutating the collection in-place? – arcseldon Jun 07 '17 at 23:32
  • 3
    Something like `db.collection.update( { "meals.name" : "breakfast" }, { $unset: { "meals.$.fruits.favourite": "" } } )`. Use `{multi: true}` for removing from multiple documents. – s7vr Jun 08 '17 at 00:16
  • @Veeram - perfect. you want to copy/paste into an answer for everyone's benefit - and I shall mark as correct. – arcseldon Jun 08 '17 at 01:11
  • @neil-lunn - please see my justification for not considering this a duplicate answer. I had researched and referenced the other SOF question prior to asking. Genuinely feel this question is helpful and should not be marked as duplicate based on the other question alone. – arcseldon Jun 08 '17 at 04:18
  • Okay then. It's instead a duplicate of [In mongoDb, how do you remove an array element by its index](https://stackoverflow.com/q/4588303/2313887) then. And I do not see a reason to remove a hold simply to mark as the other duplicate. – Neil Lunn Jun 08 '17 at 04:22
  • @NeilLunn - Nope, that is a different scenario too. And would not have helped me get the succinct reply I did - by formulating the question I had here precisely enough to get to needed answer. By all means keep trawling historical SOF Mongo questions - googled as best I could but cannot say there is not an actual duplicate out there. But you have not found it yet either. – arcseldon Jun 08 '17 at 14:58

0 Answers0