On this page本页内容
db.collection.
deleteOne
()¶mongo
Shell Method方法
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
Removes a single document from a collection.从集合中删除单个文档。
filter | document |
|
writeConcern | document |
|
collation | document |
The collation option
|
hint | document |
|
Returns: |
|
---|
db.collection.deleteOne
deletes the first document that matches the filter. 删除与筛选器匹配的第一个文档。Use a field that is part of a unique index such as 使用作为唯一索引(如_id
for precise deletions._id
)一部分的字段进行精确删除。
db.collection.deleteOne()
throws a WriteError
exception if used on a capped collection. dbcollectiondeleteOne()
如果用于封顶集合,则db.collection.deleteOne()
会引发WriteError
异常。To remove documents from a capped collection, use 要从封顶集合中删除文档,请改用db.collection.drop()
instead.db.collection.drop()
。
对分片集合执行的db.collection.deleteOne()
operations on a sharded collection must include the shard key or the _id
field in the query specification. db.collection.deleteOne()
操作必须包括查询规范中的分片键或_id
字段。分片集合中如果不包含分片键或db.collection.deleteOne()
operations in a sharded collection which do not contain either the shard key or the _id
field return an error._id
字段,则执行db.collectiond.eleteOne()
操作将返回错误。
db.collection.deleteOne()
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.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
The orders
collection has documents with the following structure:orders
集合包含具有以下结构的文档:
The following operation deletes the order with 以下操作删除带有_id: ObjectId("563237a41a4d68582c2509da")
:_id: ObjectId("563237a41a4d68582c2509da")
的订单:
The operation returns:操作返回:
The following operation deletes the first document with 以下操作将删除expiryts
greater than ISODate("2015-11-01T12:40:15Z")
expiryts
大于ISODate("2015-11-01T12:40:15Z")
的第一个文档
The operation returns:操作返回:
deleteOne()
与写关注点¶Given a three member replica set, the following operation specifies a 给定一个由三个成员组成的副本集,以下操作指定w
of majority
, wtimeout
of 100
:w
为majority
,wtimeout
为100
:
If the acknowledgement takes longer than the 如果确认时间超过wtimeout
limit, the following exception is thrown:wtimeout
限制,则会引发以下异常:
See also参阅
New in version 3.4.版本3.4中的新功能。
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
集合包含以下文档:
The following operation includes the collation option:以下操作包括排序规则选项:
hint
for Delete Operationshint
¶New in version 4.4.版本4.4中的新功能。
From the 在mongo
shell, create a members
collection with the following documents:mongo
shell中,创建包含以下文档的members
集合:
Create the following indexes on the collection:在集合上创建以下索引:
The following delete operation explicitly hints to use the index 以下删除操作明确提示使用索引{ status: 1 }
:{ status: 1 }
:
Note
If you specify an index that does not exist, the operation errors.如果指定的索引不存在,则操作将出错。
The delete command returns the following:delete命令返回以下内容:
To view the indexes used, you can use the 要查看使用的索引,可以使用$indexStats
pipeline:$indexStats
管道:
The accesses.ops
field in the $indexStats
output indicates the number of operations that used the index.$indexStats
输出中的accesses.ops
字段指示使用索引的操作数。
See also参阅
To delete multiple documents, see 要删除多个文档,请参阅db.collection.deleteMany()