- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.deleteOne()
db.collection.deleteOne()¶
On this page
Definition¶
-
db.collection.
deleteOne
()¶ mongo
Shell MethodThis page documents the
mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.Removes a single document from a collection.
Parameter Type Description filter document Specifies deletion criteria using query operators.
Specify an empty document
{ }
to delete the first document returned in the collection.writeConcern document Optional. A document expressing the write concern. Omit to use the default write concern.
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
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:
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.
hint document Optional. A document or string that specifies the index to use to support the query predicate.
The option can take an index specification document or the index name string.
If you specify an index that does not exist, the operation errors.
For an example, see Specify hint for Delete Operations.
New in version 4.4.
Returns: A document containing: - A boolean
acknowledged
astrue
if the operation ran with write concern orfalse
if write concern was disabled deletedCount
containing the number of deleted documents
- A boolean
Behavior¶
Deletion Order¶
db.collection.deleteOne
deletes the first document that matches
the filter. Use a field that is part of a unique index such as _id
for precise deletions.
Capped Collections¶
db.collection.deleteOne()
throws a WriteError
exception
if used on a capped collection. To remove documents from a capped
collection, use db.collection.drop()
instead.
Sharded Collections¶
db.collection.deleteOne()
operations on a sharded collection
must include the shard key or the _id
field in the query
specification. db.collection.deleteOne()
operations in a
sharded collection which do not contain either the shard key or
the _id
field return an error.
Transactions¶
db.collection.deleteOne()
can be used inside multi-document transactions.
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
Examples¶
Delete a Single Document¶
The orders
collection has documents with the following structure:
The following operation deletes the order with _id:
ObjectId("563237a41a4d68582c2509da")
:
The operation returns:
The following operation deletes the first document with expiryts
greater
than ISODate("2015-11-01T12:40:15Z")
The operation returns:
deleteOne() with Write Concern¶
Given a three member replica set, the following operation specifies a
w
of majority
, wtimeout
of 100
:
If the acknowledgement takes longer than the wtimeout
limit, the following
exception is thrown:
See also
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:
The following operation includes the collation option:
Specify hint
for Delete Operations¶
New in version 4.4.
From the mongo
shell, create a members
collection
with the following documents:
Create the following indexes on the collection:
The following delete operation explicitly hints to use the index
{ status: 1 }
:
Note
If you specify an index that does not exist, the operation errors.
The delete command returns the following:
To view the indexes used, you can use the $indexStats
pipeline:
The accesses.ops
field in the $indexStats
output
indicates the number of operations that used the index.
See also
To delete multiple documents, see
db.collection.deleteMany()