db.collection.deleteOne()

Definition

  • db.collection.deleteOne()

mongo Shell Method

This page documents the mongo shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.

Removes a single document from a collection.

  1. db.collection.deleteOne(
  2. <filter>,
  3. {
  4. writeConcern: <document>,
  5. collation: <document>
  6. }
  7. )

ParameterTypeDescriptionfilterdocumentSpecifies deletion criteria using query operators.

Specify an empty document { } to delete the first document returned inthe collection.writeConcerndocumentOptional. 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 ina transaction. To use write concern with transactions, seeTransactions and Write Concern.collationdocumentOptional.

Specifies the collation to use for the operation.

Collation allows users to specifylanguage-specific rules for string comparison, such as rules forlettercase and accent marks.

The collation option has the following syntax:

  1. collation: {
  2. locale: <string>,
  3. caseLevel: <boolean>,
  4. caseFirst: <string>,
  5. strength: <int>,
  6. numericOrdering: <boolean>,
  7. alternate: <string>,
  8. maxVariable: <string>,
  9. backwards: <boolean>
  10. }

When specifying collation, the locale field is mandatory; allother collation fields are optional. For descriptions of the fields,see Collation Document.

If the collation is unspecified but the collection has adefault collation (see db.createCollection()), theoperation uses the collation specified for the collection.

If no collation is specified for the collection or for theoperations, MongoDB uses the simple binary comparison used in priorversions for string comparisons.

You cannot specify multiple collations for an operation. Forexample, you cannot specify different collations per field, or ifperforming a find with a sort, you cannot use one collation for thefind and another for the sort.

New in version 3.4.

Returns:A document containing:

  • A boolean acknowledged as true if the operation ran withwrite concern or false if write concern was disabled
  • deletedCount containing the number of deleted documents

Behavior

Deletion Order

db.collection.deleteOne deletes the first document that matchesthe filter. Use a field that is part of a unique index such as _idfor precise deletions.

Capped Collections

db.collection.deleteOne() throws a WriteError exceptionif used on a capped collection. To remove documents from a cappedcollection, use db.collection.drop() instead.

Transactions

db.collection.deleteOne() can be used inside multi-document transactions.

Do not explicitly set the write concern for the operation if run ina transaction. To use write concern with transactions, seeTransactions and Write Concern.

Important

In most cases, multi-document transaction incurs a greaterperformance cost over single document writes, and theavailability of multi-document transactions should not be areplacement for effective schema design. For many scenarios, thedenormalized data model (embedded documents and arrays) will continue to be optimal for yourdata and use cases. That is, for many scenarios, modeling your dataappropriately will minimize the need for multi-documenttransactions.

For additional transactions usage considerations(such as runtime limit and oplog size limit), see alsoProduction Considerations.

Examples

Delete a Single Document

The orders collection has documents with the following structure:

  1. {
  2. _id: ObjectId("563237a41a4d68582c2509da"),
  3. stock: "Brent Crude Futures",
  4. qty: 250,
  5. type: "buy-limit",
  6. limit: 48.90,
  7. creationts: ISODate("2015-11-01T12:30:15Z"),
  8. expiryts: ISODate("2015-11-01T12:35:15Z"),
  9. client: "Crude Traders Inc."
  10. }

The following operation deletes the order with _id:ObjectId("563237a41a4d68582c2509da") :

  1. try {
  2. db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } );
  3. } catch (e) {
  4. print(e);
  5. }

The operation returns:

  1. { "acknowledged" : true, "deletedCount" : 1 }

The following operation deletes the first document with expiryts greaterthan ISODate("2015-11-01T12:40:15Z")

  1. try {
  2. db.orders.deleteOne( { "expiryts" : { $lt: ISODate("2015-11-01T12:40:15Z") } } );
  3. } catch (e) {
  4. print(e);
  5. }

The operation returns:

  1. { "acknowledged" : true, "deletedCount" : 1 }

deleteOne() with Write Concern

Given a three member replica set, the following operation specifies aw of majority, wtimeout of 100:

  1. try {
  2. db.orders.deleteOne(
  3. { "_id" : ObjectId("563237a41a4d68582c2509da") },
  4. { w : "majority", wtimeout : 100 }
  5. );
  6. } catch (e) {
  7. print (e);
  8. }

If the acknowledgement takes longer than the wtimeout limit, the followingexception is thrown:

  1. WriteConcernError({
  2. "code" : 64,
  3. "errInfo" : {
  4. "wtimeout" : true
  5. },
  6. "errmsg" : "waiting for replication timed out"
  7. })

Specify Collation

New in version 3.4.

Collation allows users to specifylanguage-specific rules for string comparison, such as rules forlettercase and accent marks.

A collection myColl has the following documents:

  1. { _id: 1, category: "café", status: "A" }
  2. { _id: 2, category: "cafe", status: "a" }
  3. { _id: 3, category: "cafE", status: "a" }

The following operation includes the collationoption:

  1. db.myColl.deleteOne(
  2. { category: "cafe", status: "A" },
  3. { collation: { locale: "fr", strength: 1 } }
  4. )

See also

To delete multiple documents, seedb.collection.deleteMany()