- Reference >
- JavaScript Methods >
- db.collection.remove()
db.collection.remove()¶
-
db.collection.
remove
(query, justOne)¶ The
remove
method removes documents from a collection.The
remove()
method can take the following parameters:Parameters: - query (document) – Optional. Specifies the deletion criteria using query
operators. Omit the
query
parameter or pass an empty document (e.g.{}
) to delete all documents in the collection. - justOne (boolean) – Optional. A boolean that limits the deletion to just one
document. The default value is
false
. Set totrue
to delete only the first result.
Note
You cannot use the
remove()
method with a capped collection.Consider the following examples of the
remove
method.To remove all documents in a collection, call the
remove
method with no parameters:This operation will remove all the documents from the collection
products
.To remove the documents that match a deletion criteria, call the
remove
method with thequery
criteria:This operation removes all the documents from the collection
products
whereqty
is greater than20
.To remove the first document that match a deletion criteria, call the
remove
method with thequery
criteria and thejustOne
parameter set totrue
or1
:This operation removes the first document from the collection
products
whereqty
is greater than20
.
Note
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. To isolate the query, include$isolated: 1
in thequery
parameter as in the following example:- query (document) – Optional. Specifies the deletion criteria using query
operators. Omit the