Definition
db.collection.deleteOne()Important
mongo Shell Method
This page documents a
mongomethod. This is not the documentation for database commands or language-specific drivers, such as Node.js. To use the database command, see thedeletecommand.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
Removes a single document from a collection.
Returns: A document containing: A boolean
acknowledgedastrueif the operation ran with write concern orfalseif write concern was disableddeletedCountcontaining the number of deleted documents
Compatibility
You can use db.collection.deleteOne() for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The deleteOne() method has the following form:
db.collection.deleteOne( <filter>, { writeConcern: <document>, collation: <document>, hint: <document|string> // Available starting in MongoDB 4.4 } )
The deleteOne() method takes the following
parameters:
Parameter | Type | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
document | Specifies deletion criteria using query operators. Specify an empty document | |||||||||||
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. | |||||||||||
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 If the collation is unspecified but the collection has a
default collation (see 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. | |||||||||||
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 New in version 4.4. |
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 distributed 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, a distributed transaction incurs a greater performance cost over single document writes, and the availability of distributed 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 distributed 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:
{ _id: ObjectId("563237a41a4d68582c2509da"), stock: "Brent Crude Futures", qty: 250, type: "buy-limit", limit: 48.90, creationts: ISODate("2015-11-01T12:30:15Z"), expiryts: ISODate("2015-11-01T12:35:15Z"), client: "Crude Traders Inc." }
The following operation deletes the order with _id:
ObjectId("563237a41a4d68582c2509da") :
try { db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } ); } catch (e) { print(e); }
The operation returns:
{ "acknowledged" : true, "deletedCount" : 1 }
The following operation deletes the first document with expiryts greater
than ISODate("2015-11-01T12:40:15Z")
try { db.orders.deleteOne( { "expiryts" : { $lt: ISODate("2015-11-01T12:40:15Z") } } ); } catch (e) { print(e); }
The operation returns:
{ "acknowledged" : true, "deletedCount" : 1 }
deleteOne() with Write Concern
Given a three member replica set, the following operation specifies a
w of majority, wtimeout of 100:
try { db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") }, { w : "majority", wtimeout : 100 } ); } catch (e) { print (e); }
If the acknowledgement takes longer than the wtimeout limit, the following
exception is thrown:
WriteConcernError({ "code" : 64, "errmsg" : "waiting for replication timed out", "errInfo" : { "wtimeout" : true, "writeConcern" : { // Added in MongoDB 4.4 "w" : "majority", "wtimeout" : 100, "provenance" : "getLastErrorDefaults" } } })
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.deleteOne( { category: "cafe", status: "A" }, { collation: { locale: "fr", strength: 1 } } )
Specify hint for Delete Operations
New in version 4.4.
In mongo, create a students collection
with the following documents:
db.members.insertMany([ { "_id" : 1, "student" : "Richard", "grade" : "F", "points" : 0 }, { "_id" : 2, "student" : "Jane", "grade" : "A", "points" : 60 }, { "_id" : 3, "student" : "Adam", "grade" : "F", "points" : 0 }, { "_id" : 4, "student" : "Ronan", "grade" : "D", "points" : 20 }, { "_id" : 5, "student" : "Noah", "grade" : "F", "points" : 0 }, { "_id" : 6, "student" : "Henry", "grade" : "A", "points" : 86 } ])
Create the following index on the collection:
db.members.createIndex( { grade: 1 } )
The following delete operation explicitly hints to use the index
{ grade: 1 }:
db.members.deleteOne( { "points": { $lte: 20 }, "grade": "F" }, { hint: { grade: 1 } } )
Note
If you specify an index that does not exist, the operation errors.
The delete command returns the following:
{ "acknowledged" : true, "deletedCount" : 1 }
To view the indexes used, you can use the $indexStats pipeline:
db.members.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } } ] )
The accesses.ops field in the $indexStats output
indicates the number of operations that used the index.
Tip
To delete multiple documents, see
db.collection.deleteMany()