OPTIONS

$pull

$pull

The $pull operator removes from an existing array all instances of a value or values that match a specified query.

db.collection.update( <query>,
                      { $pull: { <arrayField>: <query2> } }
                    )

To specify the query to remove values from the array, use query operators.

Examples

Remove All Items That Equals a Specified Value

Given the following documents in the cpuinfo collection:

{ _id: 1, flags: [ "vme", "de", "msr", "tsc", "pse", "msr" ] }
{ _id: 2, flags: [ "msr", "pse", "tsc" ] }

The following operation removes the value "msr" from the flags array:

db.cpuinfo.update(
                   { flags: "msr" },
                   { $pull: { flags: "msr" } },
                   { multi: true }
                 )

After the operation, the documents no long have any "msr" values:

{ _id: 1, flags: [  "vme",  "de",  "tsc",  "pse" ] }
{ _id: 2, flags: [  "pse",  "tsc" ] }

Remove All Items Greater Than a Specified Value

Given the following document in the profiles collection:

{ _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }

The following operation will remove all items from the votes array that are greater than or equal ($gte) 6:

db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

After the update operation, the document only has values less than 6:

{ _id: 1, votes: [  3,  5 ] }
←   $pullAll $pushAll  →