OPTIONS

$pull

$pull

The $pull operator removes all instances of a value from an existing array, as in the following prototype:

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

$pull removes items from the array in the field named field that match the query in the $pull statement.

If a value (i.e. <value>) exists multiple times in an array, $pull will remove all instances of the value.

Example

Given the following document in the cpuinfo collection:

{ flags: ['vme', 'de', 'pse', 'tsc', 'msr', 'pae', 'mce' ] }

The following operation will remove the msr value from the flags array:

db.cpuinfo.update( { flags: 'msr' }, { $pull: { flags: 'msr' } } )

Example

Given the following document in the profiles collection:

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

The following operation will remove all occurrences of 7 from the votes array.

db.profiles.update( { votes: 3 }, { $pull: { votes: 7 } } )

Therefore, the votes array would resemble the following:

{ votes: [ 3, 5, 6, 8 ] }

Conversely, the following operation will remove all items from the array that are larger than 6:

db.profiles.update( { votes: 3 }, { $pull: { votes: { $gt: 6 } } } )

Therefore, the votes array would resemble the following:

{ votes: [ 3, 5, 6 ] }
←   $pullAll $pushAll  →
ON THIS PAGE