- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.deleteOne()
db.collection.deleteOne()¶
On this page
Definition¶
-
db.collection.deleteOne()¶ Removes a single document from a collection.
Parameter Type Description filterdocument Specifies deletion criteria using query operators.
Specify an empty document
{ }to delete the first document returned in the collection.writeConcerndocument Optional. A document expressing the write concern. Omit to use the default write concern. Returns: A document containing: - A boolean
acknowledgedastrueif the operation ran with write concern orfalseif write concern was disabled deletedCountcontaining the number of deleted documents
- A boolean
Behavior¶
Deletion Order¶
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¶
deleteOne() throws a WriteError exception
if used on a capped collection. To remove documents from a capped
collection, use db.collection.drop() instead.
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
To delete multiple documents, see
db.collection.deleteMany()