OPTIONS

delete

Definition

delete

New in version 2.6.

The delete command removes documents from a collection. A single delete command can contain multiple delete specifications. The command cannot operate on capped collections. The remove methods provided by the MongoDB drivers use this command internally.

The delete command has the following syntax:

{
   delete: <collection>,
   deletes: [
      { q : <query>, limit : <integer> },
      { q : <query>, limit : <integer> },
      { q : <query>, limit : <integer> },
      ...
   ],
   ordered: <boolean>,
   writeConcern: { <write concern> }
}

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.
ordered boolean Optional. If true, then when a delete statement fails, return without performing the remaining delete statements. If false, then when a delete statement fails, continue with the remaining delete statements, if any. Defaults to true.
writeConcern document Optional. A document expressing the write concern of the delete command. Omit to use the default write concern.

Each element of the deletes array contains the following sub-fields:

Field Type Description
q string The query that matches documents to delete.
limit integer The number of matching documents to delete. Specify either a 0 to delete all matching documents or 1 to delete a single document.
Returns:A document that contains the status of the operation. See Output for details.

Behavior

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.

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:

db.runCommand(
   {
      delete: "orders",
      deletes: [ { q: { status: "D" }, limit: 1 } ]
   }
)

The returned document shows that the command deleted 1 document. See Output for details.

{ "ok" : 1, "n" : 1 }

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:

db.runCommand(
   {
      delete: "orders",
      deletes: [ { q: { status: "D" }, limit: 0 } ],
      writeConcern: { w: "majority", wtimeout: 5000 }
   }
)

The returned document shows that the command found and deleted 13 documents. See Output for details.

{ "ok" : 1, "n" : 13 }

Delete All Documents from a Collection

Delete all documents in the orders collection by specifying an empty query condition and a limit of 0:

db.runCommand(
   {
      delete: "orders",
      deletes: [ { q: { }, limit: 0 } ],
      writeConcern: { w: "majority", wtimeout: 5000 }
   }
)

The returned document shows that the command found and deleted 35 documents in total. See Output for details.

{ "ok" : 1, "n" : 35 }

Bulk Delete

The following example performs multiple delete operations on the orders collection:

db.runCommand(
   {
      delete: "orders",
      deletes: [
         { q: { status: "D" }, limit: 0 },
         { q: { cust_num: 99999, item: "abc123", status: "A" }, limit: 1 }
      ],
      ordered: false,
      writeConcern: { w: 1 }
   }
)

The returned document shows that the command found and deleted 21 documents in total for the two delete statements. See Output for details.

{ "ok" : 1, "n" : 21 }

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 writeErrors array 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 deletes array, 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 field:

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.

The following is an example document returned for a successful delete command:

{ ok: 1, n: 1 }

The following is an example document returned for a delete command that encountered an error:

{
   "ok" : 1,
   "n" : 0,
   "writeErrors" : [
      {
         "index" : 0,
         "code" : 10101,
         "errmsg" : "can't remove from a capped collection: test.cappedLog"
      }
   ]
}
←   update findAndModify  →