db.collection.deleteMany()
Definition
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 all documents that match the filter
from a collection.
- db.collection.deleteMany(
- <filter>,
- {
- writeConcern: <document>,
- collation: <document>
- }
- )
ParameterTypeDescriptionfilter
documentSpecifies deletion criteria using query operators.
To delete all documents in a collection, pass in an empty document ({ }
).writeConcern
documentOptional. 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.collation
documentOptional.
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:
- collation: {
- locale: <string>,
- caseLevel: <boolean>,
- caseFirst: <string>,
- strength: <int>,
- numericOrdering: <boolean>,
- alternate: <string>,
- maxVariable: <string>,
- backwards: <boolean>
- }
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
astrue
if the operation ran withwrite concern orfalse
if write concern was disabled
deletedCount
containing the number of deleted documents
Behavior
Capped Collections
db.collection.deleteMany()
throws a WriteError
exceptionif used on a capped collection. To remove all documents from a cappedcollection, 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
.
Transactions
db.collection.deleteMany()
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 Multiple Documents
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 all documents where client : "Crude TradersInc."
:
- try {
- db.orders.deleteMany( { "client" : "Crude Traders Inc." } );
- } catch (e) {
- print (e);
- }
The operation returns:
- { "acknowledged" : true, "deletedCount" : 10 }
The following operation deletes all documents where stock : "Brent CrudeFutures"
and limit
is greater than 48.88
:
- try {
- db.orders.deleteMany( { "stock" : "Brent Crude Futures", "limit" : { $gt : 48.88 } } );
- } catch (e) {
- print (e);
- }
The operation returns:
- { "acknowledged" : true, "deletedCount" : 8 }
deleteMany() with Write Concern
Given a three member replica set, the following operation specifies aw
of majority
and wtimeout
of 100
:
- try {
- db.orders.deleteMany(
- { "client" : "Crude Traders Inc." },
- { w : "majority", wtimeout : 100 }
- );
- } catch (e) {
- print (e);
- }
If the acknowledgement takes longer than the wtimeout
limit, the followingexception is thrown:
- WriteConcernError({
- "code" : 64,
- "errInfo" : {
- "wtimeout" : true
- },
- "errmsg" : "waiting for replication timed out"
- })
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:
- { _id: 1, category: "café", status: "A" }
- { _id: 2, category: "cafe", status: "a" }
- { _id: 3, category: "cafE", status: "a" }
The following operation includes the collationoption:
- db.myColl.deleteMany(
- { category: "cafe", status: "A" },
- { collation: { locale: "fr", strength: 1 } }
- )