w3resource

MongoDB Array Update Operator - $pullAll

Description

In MongoDB, the $pullAll operator is used to remove multiple values specified with $pullAll operator.

Syntax:

db.collection.update( { field: value }, { $pullAll: { field1: [ value1, value2, value3 ] } } );

Parameters:

Name Description
field, field1 name of the column or field to the document..
value,value1,value2,... These are the values to be specified for the fields or columns.

Sample collection "student"

{
        "_id" : 1,
        "sem" : 1,
        "subjects" : [
                "phys",
                "chem",
                "maths",
                "gkn",
                "stat",
                "astro"
        ],
        "achieve" : [
                70,
                87,
                90,
                90,
                65,
                81
        ]
}

Example of MongoDB $pullAll operator

If we want to remove the elements [ 65,87,90] specified as argument with $pullAll operator from the the array field subjects for any instance in this array is "maths", the following mongodb command can be used -

> db.student.update( { "subjects" : "gkn" }, { $pullAll: { "achieve": [65,87,90] }} );

Here in the above example the element "maths" in the array subjects will be remove, because the condition

To see the newly updated document -

> db.student.find().pretty();

Output of the command:

{
        "_id" : 1,
        "achieve" : [
                70,
                81
        ],
        "sem" : 1,
        "subjects" : [
                "phys",
                "chem",
                "maths ",
                "gkn",
                "stat",
                "astro"
        ]
}

Previous: $pull
Next: $push



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/mongodb-array-update-operator-$pullall.php