On this page本页内容
db.collection.
remove
()¶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 documents from a collection.从集合中删除文档。
The db.collection.remove()
method can have one of two syntaxes. db.collection.remove()
方法可以有两个语法之一。The remove()
method can take a query document and an optional justOne
boolean:remove()
方法可以接受查询文档和可选的justOne
布尔值:
Or the method can take a query document and an optional remove options document:或者,该方法可以获取查询文档和可选的删除选项文档:
query |
document | {} ).{} )。 |
justOne |
boolean | true . true 。false and delete all documents matching the deletion criteria.false 并删除所有符合删除条件的文档。 |
writeConcern |
document |
|
collation |
document |
Collation
|
The remove()
returns an object that contains the status of the operation.remove()
返回一个包含操作状态的对象。
WriteResult 对象。 |
The remove()
method uses the delete
command, which uses the default write concern. remove()
方法使用delete
命令,该命令使用默认的写关注点。To specify a different write concern, include the write concern in the options parameter.要指定不同的写入关注点,请在options参数中包含写入关注点。
By default, 默认情况下,remove()
removes all documents that match the query
expression. remove()
删除与query
表达式匹配的所有文档。Specify the 指定justOne
option to limit the operation to removing a single document. justOne
选项以将操作限制为删除单个文档。To delete a single document sorted by a specified order, use the findAndModify() method.要删除按指定顺序排序的单个文档,请使用findAndModify()
方法。
When removing multiple documents, the remove operation may interleave with other read and/or write operations to the collection.删除多个文档时,删除操作可能会与对集合的其他读取和/或写入操作交织。
You cannot use the 不能对封顶集合使用remove()
method with a capped collection.remove()
方法。
All 指定remove()
operations for a sharded collection that specify the justOne: true
option must include the shard key or the _id
field in the query specification. justOne:true
选项的分片集合的所有remove()
操作必须包括查询规范中的分片键或_id
字段。remove()
operations specifying justOne: true
in a sharded collection which do not contain either the shard key or the _id
field return an error.remove()
在不包含分片键或_id
字段的分片集合中指定justOne:true
的操作将返回错误。
db.collection.remove()
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 following are examples of the 下面是remove()
method.remove()
方法的示例。
To remove all documents in a collection, call the 要删除集合中的所有文档,请使用空查询文档remove
method with an empty query document {}
. {}
调用remove()
方法。The following operation deletes all documents from the bios collection:以下操作将从bios集合中删除所有文档:
This operation is not equivalent to the 此操作与drop()
method.drop()
方法不同。
To remove all documents from a collection, it may be more efficient to use the 要从集合中删除所有文档,使用drop()
method to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes.drop()
方法删除整个集合(包括索引),然后重新创建集合并重建索引可能会更有效。
To remove the documents that match a deletion criteria, call the 要删除符合删除条件的文档,请使用remove()
method with the <query>
parameter:<query>
参数调用remove()
方法:
The following operation removes all the documents from the collection 以下操作将从products
where qty
is greater than 20
:products
集合中删除qty
大于20
的所有文档:
The following operation to a replica set removes all the documents from the collection 对副本集的以下操作将从products
where qty
is greater than 20
and specifies a write concern of "w: majority"
with a wtimeout
of 5000 milliseconds such that the method returns after the write propagates to a majority of the voting replica set members or the method times out after 5 seconds.qty
大于20
的集合products
中删除所有文档,并指定一个"w: majority"
的写入关注点,wtimeout
为5000毫秒,这样,该方法在写入传播到大多数投票副本集成员后返回,或该方法在5毫秒后超时几秒钟。
To remove the first document that match a deletion criteria, call the 要删除与删除条件匹配的第一个文档,请在remove
method with the query
criteria and the justOne
parameter set to true
or 1
.query
条件和justOne
参数设置为true
或1
的情况下调用remove
方法。
The following operation removes the first document from the collection 以下操作将从products
where qty
is greater than 20
:products
集合中删除qty
大于20
的第一个文档:
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:以下操作包括collation
选项:
The remove()
returns a WriteResult
object that contains the status of the operation. remove()
返回一个WriteResult
对象,该对象包含操作的状态。Upon success, the 成功后,WriteResult
object contains information on the number of documents removed:WriteResult
对象包含有关删除的文档数的信息:
See also参阅
If the 如果remove()
method encounters write concern errors, the results include the WriteResult.writeConcernError
field:remove()
方法遇到写问题错误,结果包括WriteResult.writeConcernError
字段: