Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo $pullAll on array of objects

Currently I have a data structure like this in mongo:

{
    "_id": "field00048",
    "colorValues": [
        {
            "stateField": "Open1",
            "color": "purple"
        },
        {
            "stateField": "Open2",
            "color": "blue"
        }
    ]
}

I am using the mongo shell to create a query using $pullAll to delete both of the objects in the colorValues array by matching on the stateField field. I have tried these queries but none of them actually delete the records they match on.

db.admin.fields.update({_id: "field00048"}, {$pullAll: {stateField: ["Open1", "Open2"]}});

and

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues: ["Open1", "Open2"]}});

and

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues: [{stateField:"Open1"}, {stateField:"Open2"}]}});

and

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues.stateField: ["Open1", "Open2"]}});

and

db.admin.fields.update({_id: "field00048"}, {$pullAll: {colorValues: stateField:{["Open1", "Open2"]}}});

Any suggestions?

Thanks!

like image 444
Scott Avatar asked Dec 24 '22 09:12

Scott


1 Answers

The query that solved this was:

db.admin.fields.update({_id: "field00048"}, {$pull: {colorValues:{stateField:{$in: ["Open1", "Open2"]}}}}) 
like image 99
Scott Avatar answered Jan 06 '23 05:01

Scott