On this page本页内容
delete
The delete
command removes documents from a collection. delete
命令从集合中删除文档。A single 一个delete
command can contain multiple delete specifications. delete
命令可以包含多个删除规范。The command cannot operate on capped collections. 该命令无法对封顶集合进行操作。The remove methods provided by the MongoDB drivers use this command internally.MongoDB驱动程序提供的remove方法在内部使用此命令。
Changed in version 5.0.在版本5.0中更改。
The delete
command has the following syntax:delete
命令具有以下语法:
{ delete: <collection>, deletes: [ { q : <query>, limit : <integer>, collation: <document>, hint: <document|string> }, ... ], comment: <any>, let: <document>, // Added in MongoDB 5.0 ordered: <boolean>, writeConcern: { <write concern> } }
The command takes the following fields:该命令包含以下字段:
delete | string |
|
deletes | array |
|
comment | any |
|
let | document |
{ <variable_name_1>: <expression_1>, ..., <variable_name_n>: <expression_n> }
|
ordered | boolean |
|
writeConcern | document |
|
Each element of the deletes
array contains the following fields:deletes
数组的每个元素都包含以下字段:
q | document |
|
limit | integer |
|
collation | document |
collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> }
|
hint | Document or string |
|
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.limit:1
选项的分片集合的所有delete
操作必须在查询规范中包含分片键或_id
字段。
delete
operations specifying 在不包含分片键或limit: 1
in a sharded collection which do not contain either the shard key or the _id
field return an error._id
字段的shared集合中指定limit:1
的操作返回错误。
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.deletes
数组中所有查询的总大小(即q
字段值)必须小于或等于最大BSON文档大小。
The total number of delete documents in the deletes
array must be less than or equal to the maximum bulk size.deletes
数组中的删除文档总数必须小于或等于最大批量大小。
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.要在事务中使用写关注点,请参阅事务和写关注点。
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.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
The following example deletes from the 以下示例通过指定orders
collection one document that has the status
equal to D
by specifying the limit
of 1
:limit
为1
,从orders
集合中删除一个status
等于D
的文档:
db.runCommand( { delete: "orders", deletes: [ { q: { status: "D" }, limit: 1 } ] } )
The returned document shows that the command deleted 返回的文档显示该命令删除了1
document. 1
个文档。See Output for details.有关详细信息,请参阅输出。
{ "ok" : 1, "n" : 1 }
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.limit:1
选项的分片集合的所有delete
操作必须在查询规范中包含分片键或_id
字段。
delete
operations specifying 在不包含分片键或limit: 1
in a sharded collection which do not contain either the shard key or the _id
field return an error._id
字段的分片集合中指定limit:1
的操作返回错误。
The following example deletes from the 以下示例通过指定orders
collection all documents that have the status
equal to D
by specifying the limit
of 0
:limit
为0
,从orders
集合中删除status
等于D
的所有文档:
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. 13
个文档。See Output for details.有关详细信息,请参阅输出。
{ "ok" : 1, "n" : 13 }
Delete all documents in the 通过指定空查询条件和orders
collection by specifying an empty query condition and a limit
of 0
:limit
为0
来删除orders
集合中的所有文档:
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. 35
个文档。See Output for details.有关详细信息,请参阅输出。
{ "ok" : 1, "n" : 35 }
The following example performs multiple delete operations on the 以下示例对orders
collection:orders
集合执行多个删除操作:
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. delete
语句总共找到并删除了21
个文档。See Output for details.有关详细信息,请参阅输出。
{ "ok" : 1, "n" : 21 }
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:myColl
集合包含以下文档:
{ _id: 1, category: "café", status: "A" } { _id: 2, category: "cafe", status: "a" } { _id: 3, category: "cafE", status: "a" }
The following operation includes the collation option:以下操作包括collation
选项:
db.runCommand({ delete: "myColl", deletes: [ { q: { category: "cafe", status: "a" }, limit: 0, collation: { locale: "fr", strength: 1 } } ] })
hint
for Delete Operationshint
New in version 4.4.在版本4.4中新增。
In 在mongosh
, create a members
collection with the following documents:mongosh
中,使用以下文档创建members
集合:
db.members.insertMany([ { "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null }, { "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }, { "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null }, { "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null }, { "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null }, { "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" } ])
Create the following indexes on the collection:在集合上创建以下索引:
db.members.createIndex( { status: 1 } ) db.members.createIndex( { points: 1 } )
The following delete operation explicitly hints to use the index 以下删除操作明确提示使用索引{ status: 1 }
:{ status: 1 }
:
db.runCommand({ delete: "members", deletes: [ { q: { "points": { $lte: 20 }, "status": "P" }, limit: 0, hint: { status: 1 } } ] })
If you specify an index that does not exist, the operation errors.如果指定的索引不存在,则操作会出错。
To see the index used, run 要查看使用的索引,请对操作运行explain
on the operation:explain
:
db.runCommand( { explain: { delete: "members", deletes: [ { q: { "points": { $lte: 20 }, "status": "P" }, limit: 0, hint: { status: 1 } } ] }, verbosity: "queryPlanner" } )
let
let
中使用变量New in version 5.0.在版本5.0中新增。
To define variables that you can access elsewhere in the command, use the let option.要定义可以在命令的其他位置访问的变量,请使用let
选项。
Create a collection 创建集合cakeFlavors
:cakeFlavors
:
db.cakeFlavors.insertMany( [ { _id: 1, flavor: "chocolate" }, { _id: 2, flavor: "strawberry" }, { _id: 3, flavor: "cherry" } ] )
The following example defines a 以下示例在targetFlavor
variable in let
and uses the variable to delete the strawberry cake flavor:let
中定义了targetFlavor
变量,并使用该变量删除草莓蛋糕口味:
db.runCommand( { delete: db.cakeFlavors.getName(), deletes: [ { q: { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } }, limit: 1 } ], let : { targetFlavor: "strawberry" } } )
The returned document contains a subset of the following fields:返回的文档包含以下字段的子集:
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.writeErrors
数组包含每个出错的delete语句的错误文档。
Each error document contains the following information:每个错误文档包含以下信息:
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.errInfo.writeConcern
New in version 4.4.在版本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
). provenance
)。The following table shows the possible values for this field and their significance:下表显示了该字段的可能值及其重要性:
Provenance | |
---|---|
clientSupplied | |
customDefault | setDefaultRWConcern .setDefaultRWConcern 。
|
getLastErrorDefaults | settings.getLastErrorDefaults field.settings.getLastErrorDefaults 字段。
|
implicitDefault |
The following is an example document returned for a successful 以下是为成功的delete
command:delete
命令返回的示例文档:
{ ok: 1, n: 1 }
The following is an example document returned for a 以下是为遇到错误的delete
command that encountered an error:delete
命令返回的示例文档:
{ "ok" : 1, "n" : 0, "writeErrors" : [ { "index" : 0, "code" : 10101, "errmsg" : "can't remove from a capped collection: test.cappedLog" } ] }