- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.deleteMany()
db.collection.deleteMany()¶
On this page
Definition¶
-
db.collection.deleteMany()¶ Removes all documents that match the
filterfrom a collection.Parameter Type Description filterdocument Specifies deletion criteria using query operators.
To delete all documents in a collection, pass in an empty document (
{ }).writeConcerndocument Optional. A document expressing the write concern. Omit to use the default write concern. collationdocument 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
localefield 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.
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¶
Capped Collections¶
deleteMany() throws a WriteError exception
if used on a capped collection. To remove all documents from a capped
collection, use db.collection.drop() instead.
Delete a Single Document¶
To delete a single document, use db.collection.deleteOne() instead.
Alternatively, use a field that is a part of a unique index such as
_id.
Examples¶
Delete Multiple Documents¶
The orders collection has documents with the following structure:
The following operation deletes all documents where client : "Crude Traders
Inc.":
The operation returns:
The following operation deletes all documents where stock : "Brent Crude
Futures" and limit is greater than 48.88:
The operation returns:
deleteMany() with Write Concern¶
Given a three member replica set, the following operation specifies a
w of majority and wtimeout of 100:
If the acknowledgement takes longer than the wtimeout limit, the following
exception is thrown: