Delete Documents删除文档
On this page本页内容
The MongoDB shell provides the following methods to delete documents from a collection:MongoDB shell提供了以下方法来从集合中删除文档:
To delete multiple documents, use要删除多个文档,请使用db.collection.deleteMany()
.db.collection.deleteMany()
。To delete a single document, use要删除单个文档,请使用db.collection.deleteOne()
.db.collection.deleteOne()
。
The examples on this page reference the Atlas sample dataset. 本页的示例引用了Atlas示例数据集。You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. 您可以创建一个免费的Atlas集群,并使用示例数据填充该集群,以配合这些示例。To learn more, see Get Started with Atlas.要了解更多信息,请参阅Atlas入门。
Delete All Documents删除所有文档
To delete all documents from a collection, pass an empty filter document 若要从集合中删除所有文档,请将空筛选文档{}
to the db.collection.deleteMany()
method.{}
传递给db.collection.deleteMany()
方法。
To delete all documents from the 要从sample_mflix.movies
collection:sample_mflix.movies
集合中删除所有文档,请执行以下操作:
use sample_mflix
db.movies.deleteMany({})
The method returns a document with the status of the operation. 该方法返回一个具有操作状态的文档。For more information and examples, see 有关更多信息和示例,请参阅deleteMany()
.deleteMany()
。
Delete All Documents that Match a Condition删除符合条件的所有文档
You can specify criteria, or filters, that identify the documents to delete. 您可以指定用于标识要删除的文档的条件或筛选器。The filters use the same syntax as read operations.筛选器使用与读取操作相同的语法。
To specify equality conditions, use 若要指定相等条件,请在查询筛选器文档中使用<field>:<value>
expressions in the query filter document.<field>:<value>
表达式。
To delete all documents that match a deletion criteria, pass a filter parameter to the 要删除所有符合删除条件的文档,请向deleteMany()
method.deleteMany()
方法传递一个筛选器参数。
To delete all documents from the 要删除sample_mflix.movies
collection where the title
equals "Titanic"
:sample_mflix.movies
集合中title
等于"Titanic"
的所有文档,请执行以下操作:
use sample_mflix
db.movies.deleteMany( { title: "Titanic" } )
The method returns a document with the status of the operation. 该方法返回一个具有操作状态的文档。For more information and examples, see 有关更多信息和示例,请参阅deleteMany()
.deleteMany()
。
Delete Only One Document that Matches a Condition只删除一个符合条件的文档
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the 要删除最多一个与指定筛选器匹配的文档(即使多个文档可能与指定的筛选器匹配),请使用db.collection.deleteOne()
method.db.collection.deleteOne()
方法。
To delete the first document from the 要删除sample_mflix.movies
collection where the cast
array contains "Brad Pitt"
:sample_mflix.movies
集合中cast
数组包含"Brad Pitt"
的第一个文档,请执行以下操作:
use sample_mflix
db.movies.deleteOne( { cast: "Brad Pitt" } )
MongoDB preserves a natural sort order for documents. MongoDB保留了文档的自然排序顺序。This ordering is an internal implementation feature, and you should not rely on any particular structure within it. 这种排序是一种内部实现特性,不应该依赖于其中的任何特定结构。To learn more, see natural order.要了解更多信息,请参阅自然顺序。
Delete Behavior删除行为
To learn more about the specific behavior of deleting documents, see Behavior.要了解有关删除文档的特定行为的详细信息,请参阅行为。
Learn More了解更多信息
To see additional examples of deleting documents, see the following method pages:要查看删除文档的其他示例,请参阅以下方法页面:To see all available methods to delete documents, see Delete Methods.要查看删除文档的所有可用方法,请参阅删除方法。