- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.remove()
db.collection.remove()¶
On this page
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 isfalse
.
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:
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
:
The following operation removes all the documents from the collection
products
where qty
is greater than 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
:
The following operation removes the first document from the collection
products
where qty
is greater than 20
:
Isolate Remove Operation¶
To isolate the query, include $isolated: 1
in the
<query>
parameter as in the following examples: