Docs HomeMongoDB Manual

Delete Documents


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


This page uses the following MongoDB Node.js Driver methods:此页面使用以下MongoDB Node.js驱动程序方法:

The examples on this page use the inventory collection. 此页面上的示例使用inventory集合。Connect to a test database in your MongoDB instance then create the inventory collection:连接到MongoDB实例中的测试数据库,然后创建inventory集合:

await db.collection('inventory').insertMany([
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'P'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
}
]);

Delete All Documents删除所有文档

To delete all documents from a collection, pass an empty filter document {} to the Collection.deleteMany() method.若要从集合中删除所有文档,请将空筛选器文档{}传递给Collection.deleteMany()方法。

The following example deletes all documents from the inventory collection:以下示例从inventory集合中删除所有文档:

await db.collection('inventory').deleteMany({});

deleteMany() returns a promise that provides a result. deleteMany()返回一个提供result的promise。The result.deletedCount property contains the number of documents that matched the filter.result.deletedCount属性包含与筛选器匹配的文档数。

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>表达式:

{ <field1>: <value1>, ... }

A query filter document can use the query operators to specify conditions in the following form:查询筛选文档可以使用查询运算符以以下形式指定条件:

{ <field1>: { <operator1>: <value1> }, ... }

To delete all documents that match a deletion criteria, pass a filter parameter to the deleteMany() method.要删除所有符合删除条件的文档,请向deleteMany()方法传递一个筛选器参数。

The following example removes all documents from the inventory collection where the status field equals "A":以下示例从inventory集合中删除status字段等于"A"的所有文档:

await db.collection('inventory').deleteMany({ status: 'A' });

deleteMany() returns a promise that provides a result. deleteMany()返回一个提供result的promise。The result.deletedCount property contains the number of documents that matched the filter.result.deletedCount属性包含与筛选器匹配的文档数。

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 Collection.deleteOne() method.要删除最多一个与指定筛选器匹配的文档(即使多个文档可能与指定的筛选器匹配),请使用Collection.deleteOne()方法。

The following example deletes the first document where status is "D":以下示例删除status"D"第一个文档:

await db.collection('inventory').deleteOne({ status: 'D' });

Delete Behavior删除行为

Indexes索引

Delete operations do not drop indexes, even if deleting all documents from a collection.删除操作不会删除索引,即使从集合中删除所有文档也是如此。

Atomicity原子性

All write operations in MongoDB are atomic on the level of a single document. MongoDB中的所有写操作都是单个文档级别上的原子操作。For more information on MongoDB and atomicity, see Atomicity and Transactions.有关MongoDB和原子性的更多信息,请参阅原子性和事务

Write Acknowledgement书面确认

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. 对于写入关注,您可以指定MongoDB为写操作请求的确认级别。For details, see Write Concern.有关详细信息,请参阅写入关注