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. You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. To learn more, see Get Started with Atlas.本页上的示例引用了Atlas示例数据集。您可以创建一个免费的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()方法。
Example示例
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()。
Note
If you want to delete all documents from a large collection, dropping with the 如果要删除大型集合中的所有文档,请使用db.collection.drop() method. and recreating the collection may have faster performance than deleting documents with the db.collection.deleteMany() method. When you recreate the collection, you must also recreate any specified collection parameters such as collection indexes.db.collection.drop()方法删除。重新创建集合的性能可能比使用db.collection.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()方法。
Example示例
To delete all documents from the 要删除sample_mflix.movies collection where the title equals "Titanic":sample_mflix.movies集合中标题等于"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()方法。
Example示例
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" } )
Note
MongoDB preserves a natural sort order for documents. This ordering is an internal implementation feature, and you should not rely on any particular structure within it. To learn more, see natural order.MongoDB保留了文档的自然排序顺序。这种排序是一种内部实现特性,您不应该依赖其中的任何特定结构。要了解更多信息,请参阅自然顺序。
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.要查看删除文档的所有可用方法,请参阅删除方法。