- Reference >
- Database Commands >
- Query and Write Operation Commands >
- delete
delete¶
On this page
Definition¶
-
delete¶ The
deletecommand removes documents from a collection. A singledeletecommand can contain multiple delete specifications. The command cannot operate on capped collections. The remove methods provided by the MongoDB drivers use this command internally.Changed in version 5.0.
The
deletecommand has the following syntax:The command takes the following fields:
Field Type Description delete string The name of the target collection.
deletes array An array of one or more delete statements to perform in the named collection.
commentany Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
- mongod log messages, in the
attr.command.cursor.commentfield. - Database profiler output, in the
command.commentfield. currentOpoutput, in thecommand.commentfield.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
let document Optional.
Specifies a document with a list of variables. This allows you to improve command readability by separating the variables from the query text.
The document syntax is:
The variable is set to the value returned by the expression, and cannot be changed afterwards.
To access the value of a variable in the command, use the double dollar sign prefix (
$$) together with your variable name in the form$$<variable_name>. For example:$$targetTotal.Note
To use a variable to filter results, you must access the variable within the
$exproperator.For a complete example using
letand variables, see Use Variables in let.New in version 5.0.
ordered boolean Optional. If
true, then when a delete statement fails, return without performing the remaining delete statements. Iffalse, then when a delete statement fails, continue with the remaining delete statements, if any. Defaults totrue.writeConcern document Optional. A document expressing the write concern of the
deletecommand. 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.
Each element of the
deletesarray contains the following fields:Field Type Description q document The query that matches documents to delete.
limit integer The number of matching documents to delete. Specify either a
0to delete all matching documents or1to delete a single document.collation 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
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.
hint Document or string 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 hint for Delete Operations.
New in version 4.4.
Returns: A document that contains the status of the operation. See Output for details. - mongod log messages, in the
Behavior¶
Sharded Collections¶
All delete operations for a sharded
collection that specify the limit: 1
option must include the shard key or the _id field in
the query specification.
delete operations specifying
limit: 1 in a sharded collection which do
not contain either the shard key or the _id field return an
error.
Limits¶
The total size of all the queries (i.e. the q field values) in the
deletes array must be less than or equal to the maximum
BSON document size.
The total number of delete documents in the deletes array must be
less than or equal to the maximum bulk size.
Transactions¶
delete can be used inside multi-document 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, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document 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 multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
Examples¶
Limit the Number of Documents Deleted¶
The following example deletes from the orders collection one
document that has the status equal to D by specifying the
limit of 1:
The returned document shows that the command deleted 1 document.
See Output for details.
Delete All Documents That Match a Condition¶
The following example deletes from the orders collection all
documents that have the status equal to D by specifying the
limit of 0:
The returned document shows that the command found and deleted 13
documents. See Output for details.
Delete All Documents from a Collection¶
Delete all documents in the orders collection by specifying an
empty query condition and a limit of 0:
The returned document shows that the command found and deleted 35
documents in total. See Output for details.
Bulk Delete¶
The following example performs multiple delete operations on the
orders collection:
The returned document shows that the command found and deleted 21
documents in total for the two delete statements. See
Output for details.
Specify Collation¶
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:
The following operation includes the collation option:
Specify hint for Delete Operations¶
New in version 4.4.
In mongosh, create a members collection
with the following documents:
Create the following indexes on the collection:
The following delete operation explicitly hints to use the index
{ status: 1 }:
Note
If you specify an index that does not exist, the operation errors.
To see the index used, run explain on the operation:
Use Variables in let¶
New in version 5.0.
To define variables that you can access elsewhere in the command, use the let option.
Note
To filter results using a variable, you must access the variable
within the $expr operator.
Create a collection cakeFlavors:
The following example defines a targetFlavor variable in let and
uses the variable to delete the strawberry cake flavor:
Output¶
The returned document contains a subset of the following fields:
-
delete.ok¶ The status of the command.
-
delete.n¶ The number of documents deleted.
-
delete.writeErrors¶ An array of documents that contains information regarding any error encountered during the delete operation. The
writeErrorsarray contains an error document for each delete statement that errors.Each error document contains the following information:
-
delete.writeErrors.index¶ An integer that identifies the delete statement in the
deletesarray, which uses a zero-based index.
-
delete.writeErrors.code¶ An integer value identifying the error.
-
delete.writeErrors.errmsg¶ A description of the error.
-
-
delete.writeConcernError¶ Document that describe error related to write concern and contains the fields:
-
delete.writeConcernError.code¶ An integer value identifying the cause of the write concern error.
-
delete.writeConcernError.errmsg¶ A description of the cause of the write concern error.
-
delete.writeConcernError.errInfo.writeConcern¶ New in version 4.4.
The write concern object used for the corresponding operation. For information on write concern object fields, see Write Concern Specification.
The write concern object may also contain the following field, indicating the source of the write concern:
-
delete.writeConcernError.errInfo.writeConcern.provenance¶ A string value indicating where the write concern originated (known as write concern
provenance). The following table shows the possible values for this field and their significance:Provenance Description clientSuppliedThe write concern was specified in the application. customDefaultThe write concern originated from a custom defined default value. See setDefaultRWConcern.getLastErrorDefaultsThe write concern originated from the replica set’s settings.getLastErrorDefaultsfield.implicitDefaultThe write concern originated from the server in absence of all other write concern specifications.
-
-
The following is an example document returned for a successful
delete command:
The following is an example document returned for a delete
command that encountered an error: