Delete a Document删除文档
On this page本页内容
Overview概述
In this section, we show you how to call the write operations to remove documents from a collection in your MongoDB database.在本节中,我们将向您展示如何调用写操作来从MongoDB数据库中的集合中删除文档。
Delete删除
If you want to remove existing documents from a collection, you can use 如果要从集合中删除现有文档,可以使用deleteOne()
to remove one document or deleteMany()
for one or more documents. deleteOne()
来删除一个文档,也可以使用deleteMany()
来移除一个或多个文档。These methods accept a query document that matches the documents you want to delete.这些方法接受与要删除的文档匹配的查询文档。
You can specify the document or documents to be deleted by the 您可以在JSON对象中指定deleteOne()
or deleteMany()
write operations in a JSON object as follows:deleteOne()
或deleteMany()
写入操作要删除的一个或多个文档,如下所示:
const doc = {
pageViews: {
$gt: 10,
$lt: 32768
}
};
To delete the first matching document using the 要使用deleteOne()
method or to delete all matching documents using the deleteMany()
method, pass the document as the method parameter:deleteOne()
方法删除第一个匹配文档,或使用deleteMany()
方法移除所有匹配文档,请将该文档作为方法参数传递:
const deleteResult = await myColl.deleteOne(doc);
const deleteManyResult = await myColl.deleteMany(doc);
You can print the number of documents deleted by the operation by accessing the 通过访问上面每个方法调用的结果的deletedCount
field of the result for each of the method calls above as follows:deletedCount
字段,可以打印操作删除的文档数,如下所示:
console.dir(deleteResult.deletedCount);
console.dir(deleteManyResult.deletedCount);
Upon successful delete, these statements should print the number of documents deleted by the associated operation.成功删除后,这些语句应打印关联操作删除的文档数。
For fully runnable examples and additional information on the available options, see our usage examples for deleteOne() and deleteMany().有关完全可运行的示例和可用选项的其他信息,请参阅deleteOne()
和deleteMany()
的使用示例。