w3resource

MongoDB Array Update Operator - $pull

Description

In MongoDB, the $pull operator is used to removing all instances of a value from an existing array.

Syntax:

db.collection.update( { field: <query> }, { $pull: { field: <query> } } );

Parameters:

Name Description
field name of the column or field to the document..
query This is an expression or condition.

Sample collection "student"

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

Example of MongoDB $pull operator

If we want to remove the element "maths" 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" : "maths" }, { $pull: { "subjects": "maths" }} );

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,
                87,
                90,
                90,
                65,
                81
        ],
        "sem" : 1,
        "subjects" : [
                "phys",
                "chem",
                "gkn",
                "stat",
                "astro"
        ]
}

Example of MongoDB $pull to remove all occurrence

If we want to remove all the elements 70 from the array field achieve for any instance of "gkn " in the array subjects, the following mongodb command can be used -

> db.student.update( { "subjects" : "gkn" }, { $pull: { "achieve": 90 }} );

Here in the above example all the elements 90 in the array achieve have removed for any instance of "gkn" in the array subjects.

To see the newly updated document -

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

Output of the command:

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

Example of MongoDB $pull using $gte operator

If we want to remove all the elements greater than equal to 85 from the array field achieve for any instance of "gkn " in the array subjects, the following mongodb command can be used -

> db.student.update( { "subjects" : "gkn" }, { $pull: { "achieve":{$gte :85} }});

Here in the above example the all the elements greater than equal to 85 in the array achieve have removed for any instance of "gkn" in the array subjects.

To see the newly updated document -

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

Output of the command:

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

Previous: $pop
Next: $pullAll



Follow us on Facebook and Twitter for latest update.