- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.remove()
db.collection.remove()¶
On this page
Definition¶
-
db.collection.
remove
()¶ Removes documents from a collection.
The
db.collection.remove()
method can have one of two syntaxes. Theremove()
method can take a query document and an optionaljustOne
boolean:db.collection.remove( <query>, <justOne> )
Or the method can take a query document and an optional remove options document:
New in version 2.6.
db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document>, collation: <document> } )
Parameter Type Description query
document Specifies deletion criteria using query operators. To delete all documents in a collection, pass an empty document (
{}
).Changed in version 2.6: In previous versions, the method invoked with no query parameter deleted all documents in a collection.
justOne
boolean Optional. To limit the deletion to just one document, set to true
. Omit to use the default value offalse
and delete all documents matching the deletion criteria.writeConcern
document Optional. A document expressing the write concern. Omit to use the default write concern. See Write Concern.
New in version 2.6.
collation
document Optional.
Specifies the collation to use for the operation.
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.
The collation option has the following syntax:
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }
When specifying collation, the
locale
field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.If the collation is unspecified but the collection has a default collation (see
db.createCollection()
), the operation uses the collation specified for the collection.If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons.
You cannot specify multiple collations for an operation. For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort.
New in version 3.4.
Changed in version 2.6: The
remove()
returns an object that contains the status of the operation.Returns: A WriteResult object that contains the status of the operation.
Behavior¶
Write Concern¶
Changed in version 2.6.
The remove()
method uses the
delete
command, which uses the default write concern. To specify a different write concern,
include the write concern in the options parameter.
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 Operations 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 an empty query document {}
.
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 removes all the documents from the collection
products
where qty
is greater than 20
:
db.products.remove( { qty: { $gt: 20 } } )
Override Default Write Concern¶
The following operation to a replica set removes all the documents from
the collection products
where qty
is greater than 20
and
specifies a write concern of "w:
majority"
with a wtimeout
of 5000 milliseconds such that the
method returns after the write propagates to a majority of the voting
replica set members or the method times out after 5 seconds.
Changed in version 3.0: In previous versions, majority
referred to the majority of all
members of the replica set instead of the majority of the voting
members.
db.products.remove(
{ qty: { $gt: 20 } },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)
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 removes the first document from the collection
products
where qty
is greater than 20
:
db.products.remove( { qty: { $gt: 20 } }, true )
Isolate Remove Operations¶
To isolate the query, include $isolated: 1
in the
<query>
parameter as in the following examples:
db.products.remove( { qty: { $gt: 20 }, $isolated: 1 } )
Specify Collation¶
New in version 3.4.
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.
A collection myColl
has the following documents:
{ _id: 1, category: "café", status: "A" }
{ _id: 2, category: "cafe", status: "a" }
{ _id: 3, category: "cafE", status: "a" }
The following operation includes the collation option:
db.myColl.remove(
{ category: "cafe", status: "A" },
{ collation: { locale: "fr", strength: 1 } }
)
WriteResult¶
Changed in version 2.6.
Successful Results¶
The remove()
returns a WriteResult
object that contains the status of the operation. Upon success, the
WriteResult
object contains information on the number of
documents removed:
WriteResult({ "nRemoved" : 4 })
See also
Write Concern Errors¶
If the remove()
method encounters write
concern errors, the results include the
WriteResult.writeConcernError
field:
WriteResult({
"nRemoved" : 21,
"writeConcernError" : {
"code" : 64,
"errInfo" : {
"wtimeout" : true
},
"errmsg" : "waiting for replication timed out"
}
})
See also