OPTIONS

db.collection.remove()

Definition

db.collection.remove(query, justOne)

Removes documents from a collection.

The remove() method has the following parameters:

Parameter Type Description
query document Optional. Specifies deletion criteria using query operators. To delete all documents in a collection, omit this parameter or pass an empty document ({}).
justOne boolean Optional. To limit the deletion to just one document, set to true. The default value is false.

Behavior

Query Considerations

By default, remove() removes all documents that match the query expression. Specify the justOne option to limit the operation to removing a single document. To delete a single document sorted by a specified order, use the findAndModify() method.

When removing multiple documents, the remove operation may interleave with other read and/or write operations to the collection. For unsharded collections, you can override this behavior with the $isolated operator, which “isolates” the remove operation and disallows yielding during the operation. This ensures that no client can see the affected documents until they are all processed or an error stops the remove operation.

See Isolate Remove Operation for an example.

Capped Collections

You cannot use the remove() method with a capped collection.

Examples

The following are examples of the remove() method.

Remove All Documents from a Collection

To remove all documents in a collection, call the remove method with no parameters: The following operation deletes all documents from the bios collection:

db.bios.remove()

This operation is not equivalent to the drop() method.

To remove all documents from a collection, it may be more efficient to use the drop() method to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes.

Remove All Documents that Match a Condition

To remove the documents that match a deletion criteria, call the remove() method with the <query> parameter:

The following operation deletes all documents from the bios collection where the subdocument name contains a field first whose value starts with G:

db.bios.remove( { 'name.first' : /^G/ } )

The following operation removes all the documents from the collection products where qty is greater than 20:

db.products.remove( { qty: { $gt: 20 } } )

Remove a Single Document that Matches a Condition

To remove the first document that match a deletion criteria, call the remove method with the query criteria and the justOne parameter set to true or 1.

The following operation deletes a single document from the bios collection where the turing field equals true:

db.bios.remove( { turing: true }, 1 )

The following operation removes the first document from the collection products where qty is greater than 20:

db.products.remove( { qty: { $gt: 20 } }, true )

Isolate Remove Operation

To isolate the query, include $isolated: 1 in the <query> parameter as in the following examples:

db.products.remove( { qty: { $gt: 20 }, $isolated: 1 } )