MongoDB provides the following methods to delete documents of a collection:MongoDB提供了以下方法来删除集合中的文档:
db.collection.deleteOne() | |
db.collection.deleteMany() | |
db.collection.remove() |
Starting in MongoDB 6.1:从MongoDB 6.1开始:
To improve efficiency, MongoDB may batch multiple document deletions together.为了提高效率,MongoDB可能会同时批量删除多个文档。Theexplaincommand results contain a newBATCHED_DELETEstage for batched document deletions.explain命令结果包含一个新的BATCHED_DELETE阶段,用于批量删除文档。
Additional Methods附加方法
The following methods can also delete documents from a collection:以下方法也可以从集合中删除文档:
db.collection.findOneAndDelete().findOneAndDelete()provides a sort option. The option allows for the deletion of the first document sorted by the specified order.提供排序选项。该选项允许删除按指定顺序排序的第一个文档。db.collection.findAndModify().db.collection.findAndModify()provides a sort option. The option allows for the deletion of the first document sorted by the specified order.提供排序选项。该选项允许删除按指定顺序排序的第一个文档。db.collection.bulkWrite().
See the individual reference pages for the methods for more information and examples.有关更多信息和示例的方法,请参阅各个参考页。