Delete Documents
This page provides examples of delete operations using the following methods in the mongo shell:
The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
db.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" },
] );
You can run the operation in the web shell below:
Delete All Documents
To delete all documents from a collection, pass an empty filter document {}
to the db.collection.deleteMany() method.
The following example deletes all documents from the inventory
collection:
db.inventory.deleteMany({})
The method returns a document with the status of the operation. For more information and examples, see 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:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给deleteMany()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
db.inventory.deleteMany({ status : "A" })
该方法返回具有操作状态的文档。有关更多信息和示例,请参见deleteMany()。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用db.collection.deleteOne()方法。
以下示例删除status
为"D"
的* first *文档:
db.inventory.deleteOne( { status: "D" } )
[!tab|label:Compass]
此页面提供了使用MongoDB Compass删除文档的示例。
使用以下文档填充inventory
集合:
[
{ 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" },
]
有关在 MongoDB Compass 中插入文档的说明,请参阅Insert Documents。
Note
有关在 MongoDB Compass 中插入文档的完整参考,请参见Compass documentation。
删除单个文件
MongoDB Compass 提供了一种从集合中删除文档的简单方法。以下示例显示如何从inventory
集合中删除item
等于paper
的文档:
Note
在此示例中,我们使用指南针Table View删除文档。使用指南针List View进行删除的过程非常相似。
有关指南针中“表视图”和“列表视图”之间差异的更多信息,请参阅Compass documentation。
- 点击顶部导航中的表格按钮,以访问Table View:
- 使用指南针query bar查找目标文档。
将以下过滤器文档复制到查询栏中,然后单击“查找”:
{ item: "paper" }
- 将鼠标悬停在文档上,然后点击右侧显示的垃圾桶图标:
单击删除按钮后,该文档被标记为要删除,Compass 要求您确认要删除该文档:
-
单击删除进行确认。指南针将从集合中删除文档。
[!tab|label:Python]
本页提供了使用PyMongo Python 驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
db.inventory.insert_many([
{"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"}])
删除所有文件
要从集合中删除所有文档,请将空的filter文档{}
传递给pymongo.collection.Collection.delete_many()方法。
以下示例从inventory
集合中删除全部文档:
db.inventory.delete_many({})
delete_many()方法返回具有操作状态的pymongo.results.DeleteResult实例。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给delete_many()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
db.inventory.delete_many({"status": "A"})
delete_many()方法返回具有操作状态的pymongo.results.DeleteResult实例。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用pymongo.collection.Collection.delete_one()方法。
以下示例删除status
为"D"
的* first *文档:
db.inventory.delete_one({"status": "D"})
[!tab|label:Java (Sync)]
本页提供了使用Java 同步驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
删除所有文件
要从集合中删除所有文档,请将空的org.bson.Document对象作为filter传递给com.mongodb.client.MongoCollection.deleteMany方法。
以下示例从inventory
集合中删除全部文档:
collection.deleteMany(new Document());
com.mongodb.client.MongoCollection.deleteMany方法返回具有操作状态的com.mongodb.client.result.DeleteResult实例。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给com.mongodb.client.MongoCollection.deleteMany方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
collection.deleteMany(eq("status", "A"));
com.mongodb.client.MongoCollection.deleteMany方法返回具有操作状态的com.mongodb.client.result.DeleteResult实例。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用com.mongodb.client.MongoCollection.deleteOne方法。
以下示例删除status
为"D"
的* first *文档:
collection.deleteOne(eq("status", "D"));
[!tab|label:Node.js]
本页提供了使用MongoDB Node.js 驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充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'
}
]);
删除所有文件
要从集合中删除所有文档,请将空的filter文档{}
传递给Collection.deleteMany()方法。
以下示例从inventory
集合中删除全部文档:
await db.collection('inventory').deleteMany({});
deleteMany()返回提供result
的承诺。 result.deletedCount
属性包含与过滤器匹配的文档数。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给deleteMany()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
await db.collection('inventory').deleteMany({ status: 'A' });
deleteMany()返回提供result
的承诺。 result.deletedCount
属性包含与过滤器匹配的文档数。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用Collection.deleteOne()方法。
以下示例删除status
为"D"
的* first *文档:
await db.collection('inventory').deleteOne({ status: 'D' });
[!tab|label:PHP]
本页提供了使用MongoDB PHP 库中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
$insertManyResult = $db->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',
],
]);
删除所有文件
要从集合中删除所有文档,请将空的filter文档[]
传递给MongoDB\Collection::deleteMany()方法。
以下示例从inventory
集合中删除全部文档:
$deleteResult = $db->inventory->deleteMany([]);
成功执行后,deleteMany()方法返回MongoDB\DeleteResult的实例,该实例的getDeletedCount()方法返回与过滤器匹配的文档数。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给deleteMany()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
$deleteResult = $db->inventory->deleteMany(['status' => 'A']);
成功执行后,deleteMany()方法返回MongoDB\DeleteResult的实例,该实例的getDeletedCount()方法返回与过滤器匹配的文档数。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用MongoDB\Collection::deleteOne()方法。
以下示例删除status
为"D"
的* first *文档:
$deleteResult = $db->inventory->deleteOne(['status' => 'D']);
[!tab|label:Motor]
此页面提供了使用Motor驱动程序中的以下方法进行删除操作的示例:
-
motor.motor_asyncio.AsyncIOMotorCollection.delete_many()
-
motor.motor_asyncio.AsyncIOMotorCollection.delete_one()
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
await db.inventory.insert_many([
{"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"}])
删除所有文件
要从集合中删除所有文档,请将空的filter文档{}
传递给motor.motor_asyncio.AsyncIOMotorCollection.delete_many()
方法。
以下示例从inventory
集合中删除全部文档:
await db.inventory.delete_many({})
delete_many()
协程异步返回具有操作状态的pymongo.results.DeleteResult实例。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给delete_many()
方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
await db.inventory.delete_many({"status": "A"})
delete_many()
协程异步返回具有操作状态的pymongo.results.DeleteResult实例。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用motor.motor_asyncio.AsyncIOMotorCollection.delete_one()
方法。
以下示例删除status
为"D"
的* first *文档:
await db.inventory.delete_one({"status": "D"})
[!tab|label:Java (Async)]
本页提供了使用Java 反应流驱动程序中的以下方法进行删除操作的示例:
-
com.mongodb.reactivestreams.client.MongoCollection.deleteMany
-
com.mongodb.reactivestreams.client.MongoCollection.deleteOne
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
删除所有文件
要从集合中删除所有文档,请将空的org.bson.Document对象作为filter传递给com.mongodb.reactivestreams.client.MongoCollection.deleteMany方法。
以下示例从inventory
集合中删除全部文档:
Publisher<DeleteResult> deleteManyPublisher = collection.deleteMany(new Document());
如果成功,com.mongodb.reactivestreams.client.MongoCollection.deleteMany返回类型为com.mongodb.client.result.DeleteResult的Publisher对象。如果不成功,则返回com.mongodb.MongoException
的实例。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给com.mongodb.reactivestreams.client.MongoCollection.deleteMany方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
deleteManyPublisher = collection.deleteMany(eq("status", "A"));
如果成功,com.mongodb.reactivestreams.client.MongoCollection.deleteMany返回类型为com.mongodb.client.result.DeleteResult的Publisher对象。如果不成功,则返回com.mongodb.MongoException
的实例。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用com.mongodb.reactivestreams.client.MongoCollection.deleteMany方法。
以下示例删除status
为"D"
的* first *文档:
Publisher<DeleteResult> deleteOnePublisher = collection.deleteOne(eq("status", "D"));
[!tab|label:C#]
本页提供了使用MongoDB C#驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
var documents = new[]
{
new BsonDocument
{
{ "item", "journal" },
{ "qty", 25 },
{ "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } },
{ "status", "A" }
},
new BsonDocument
{
{ "item", "notebook" },
{ "qty", 50 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "status", "P" }
},
new BsonDocument
{
{ "item", "paper" },
{ "qty", 100 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "planner" },
{ "qty", 75 },
{ "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm" } } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "postcard" },
{ "qty", 45 },
{ "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm" } } },
{ "status", "A" }
}
};
collection.InsertMany(documents);
删除所有文件
要删除集合中的所有文档,请将空的filter Builders<BsonDocument>.Filter.Empty
传递给IMongoCollection.DeleteMany()方法。
以下示例从inventory
集合中删除全部文档:
var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.DeleteMany(filter);
成功执行后,IMongoCollection.DeleteMany()方法返回DeleteResult的实例,该实例的DeletedCount
属性包含与过滤器匹配的文档数。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给IMongoCollection.DeleteMany()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var result = collection.DeleteMany(filter);
成功执行后,IMongoCollection.DeleteMany()方法返回DeleteResult的实例,该实例的DeletedCount
属性包含与过滤器匹配的文档数。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用IMongoCollection.DeleteOne()方法。
以下示例删除status
为"D"
的* first *文档:
var filter = Builders<BsonDocument>.Filter.Eq("status", "D");
var result = collection.DeleteOne(filter);
[!tab|label:Perl]
本页提供了使用MongoDB Perl 驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
$db->coll("inventory")->insert_many(
[
{
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"
}
]
);
删除所有文件
要从集合中删除所有文档,请将空的filter文档{}
传递给MongoDB::Collection::delete_many()方法。
以下示例从inventory
集合中删除全部文档:
$db->coll("inventory")->delete_many( {} );
成功执行后,delete_many()方法返回MongoDB::DeleteResult的实例,该实例的deleted_count
属性包含与过滤器匹配的文档数。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给delete_many()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
$db->coll("inventory")->delete_many( { status => "A" } );
成功执行后,delete_many()方法返回MongoDB::DeleteResult的实例,该实例的deleted_count
属性包含与过滤器匹配的文档数。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用MongoDB::Collection::delete_one()方法。
以下示例删除status
为"D"
的* first *文档:
$db->coll("inventory")->delete_one( { status => "D" } );
[!tab|label:Ruby]
本页提供了使用MongoDB Ruby 驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
client[:inventory].insert_many([
{ 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' },
])
删除所有文件
要从集合中删除所有文档,请将空的filter文档{}
传递给Mongo::Collection#delete_many()方法。
以下示例从inventory
集合中删除全部文档:
client[:inventory].delete_many({})
成功执行后,delete_many()方法返回Mongo::Operation::Result的实例,该实例的deleted_count
属性包含与过滤器匹配的文档数。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给delete_many()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
client[:inventory].delete_many(status: 'A')
成功执行后,delete_many()方法返回Mongo::Operation::Result的实例,该实例的deleted_count
属性包含与过滤器匹配的文档数。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用Mongo::Collection#delete_one()方法。
以下示例删除status
为"D"
的* first *文档:
client[:inventory].delete_one(status: 'D')
[!tab|label:Scala]
本页提供了使用MongoDB Scala 驱动程序中的以下方法进行删除操作的示例:
此页面上的示例使用inventory
集合。要填充inventory
集合,请运行以下命令:
collection.insertMany(Seq(
Document("""{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""),
Document("""{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" }"""),
Document("""{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }"""),
Document("""{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }"""),
Document("""{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }""")
)).execute()
删除所有文件
要删除集合中的所有文档,请将空的filter Document()
传递给collection.deleteMany()方法。
以下示例从inventory
集合中删除全部文档:
collection.deleteMany(Document()).execute()
成功执行后,collection.deleteMany()方法将返回带有单个元素和DeleteResult
类型参数或带有com.mongodb.MongoException
的Observable。
删除所有符合条件的文档
您可以指定标准或过滤器,以标识要删除的文档。 filters使用与读取操作相同的语法。
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
要删除所有符合删除条件的文档,请将filter参数传递给deleteMany()方法。
以下示例从status
字段等于"A"
的inventory
集合中删除所有文档:
collection.deleteMany(equal("status", "A")).execute()
成功执行后,collection.deleteMany()方法将返回带有单个元素和DeleteResult
类型参数或带有com.mongodb.MongoException
的Observable。
仅删除一个符合条件的文档
要删除最多一个与指定过滤器匹配的文档(即使多个文档可能与指定过滤器匹配),请使用collection.deleteOne()方法。
以下示例删除status
为"D"
的* first *文档:
collection.deleteOne(equal("status", "D")).execute()
{#write-op-remove-behavior} {#write-op-delete-behavior}
## {#delete-behavior} Delete Behavior
### {#indexes} Indexes
Delete operations do not drop indexes, even if deleting all documents from a collection\.
{#indexes}
### {#atomicity} Atomicity
All write operations in MongoDB are atomic on the level of a single document\. For more information on MongoDB and atomicity, see [Atomicity and Transactions](core-write-operations-atomicity.html)\.
{#atomicity}
### {#write-acknowledgement} Write Acknowledgement
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations\. For details, see [Write Concern](reference-write-concern.html)\.
````[tabs]
[!tab|label:Mongo Shell]
> [!NOTE|label:See also]
>
>
> - [db\.collection\.deleteMany\(\)](reference-method-db.collection.deleteMany.html#db.collection.deleteMany)
>
> - [db\.collection\.deleteOne\(\)](reference-method-db.collection.deleteOne.html#db.collection.deleteOne)
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:Compass]
> [!NOTE|label:See also]
>
>
> - [Compass Documents](https://docs.mongodb.com/compass/master/documents/)
>
> - [Compass Query Bar](https://docs.mongodb.com/compass/current/query/filter/#compass-query-bar)
>
>
[!tab|label:Python]
> [!NOTE|label:See also]
>
>
> - [pymongo\.collection\.Collection\.delete_many\(\)](https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.delete_many)
>
> - [pymongo\.collection\.Collection\.delete_one\(\)](https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.delete_one)
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:Java (Sync)]
> [!NOTE|label:See also]
>
>
> - [com\.mongodb\.client\.MongoCollection\.deleteMany](https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/MongoCollection.html#deleteMany-org.bson.conversions.Bson-)
>
> - [com\.mongodb\.client\.MongoCollection\.deleteOne](https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/MongoCollection.html#deleteOne-org.bson.conversions.Bson-)
>
> - [Additional Java Synchronous Driver Write Examples](http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-write-operations/)
>
>
[!tab|label:Node.js]
> [!NOTE|label:See also]
>
>
> - [Collection\.deleteMany\(\)](https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#deleteMany)
>
> - [Collection\.deleteOne\(\)](https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#deleteOne)
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:PHP]
> [!NOTE|label:See also]
>
>
> - [MongoDB\Collection::deleteMany\(\)](https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-deleteMany/#phpmethod.MongoDB\Collection::deleteMany)
>
> - [MongoDB\Collection::deleteOne\(\)](https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-deleteOne/#phpmethod.MongoDB\Collection::deleteOne)
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:Motor]
> [!NOTE|label:See also]
>
>
> - `motor.motor_asyncio.AsyncIOMotorCollection.delete_many()`
>
> - `motor.motor_asyncio.AsyncIOMotorCollection.delete_one()`
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:Java (Async)]
> [!NOTE|label:See also]
>
>
> - [com\.mongodb\.reactivestreams\.client\.MongoCollection\.deleteMany](http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#deleteMany(org.bson.conversions.Bson))
>
> - [com\.mongodb\.reactivestreams\.client\.MongoCollection\.deleteOne](http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#deleteOne(org.bson.conversions.Bson))
>
> - [Java Reactive Streams Driver Quick Tour](http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/getting-started/quick-tour/)
>
>
[!tab|label:C#]
> [!NOTE|label:See also]
>
>
> - [IMongoCollection\.DeleteMany\(\)](https://api.mongodb.com/csharp/current/html/M_MongoDB_Driver_IMongoCollection_1_DeleteMany.htm)
>
> - [IMongoCollection\.DeleteOne\(\)](https://api.mongodb.com/csharp/current/html/M_MongoDB_Driver_IMongoCollection_1_DeleteOne.htm)
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:Perl]
> [!NOTE|label:See also]
>
>
> - [MongoDB::Collection::delete_many\(\)](https://metacpan.org/pod/MongoDB::Collection#delete_many)
>
> - [MongoDB::Collection::delete_one\(\)](https://metacpan.org/pod/MongoDB::Collection#delete_one)
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>
[!tab|label:Ruby]
> [!NOTE|label:See also]
>
>
> - [Mongo::Collection\#delete_many\(\)](https://api.mongodb.com/ruby/current/Mongo/Collection.html#delete_many-instance_method)
>
> - [Mongo::Collection\#delete_one\(\)](https://api.mongodb.com/ruby/current/Mongo/Collection.html#delete_one-instance_method)
>
>
[!tab|label:Scala]
> [!NOTE|label:See also]
>
>
> - [collection\.deleteMany\(\)](https://mongodb.github.io/mongo-scala-driver/2.0/scaladoc/org/mongodb/scala/MongoCollection.html#deleteMany(filter:org.mongodb.scala.bson.conversions.Bson,options:org.mongodb.scala.model.DeleteOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.DeleteResult])
>
> - [collection\.deleteOne\(\)](https://mongodb.github.io/mongo-scala-driver/2.0/scaladoc/org/mongodb/scala/MongoCollection.html#deleteOne(filter:org.mongodb.scala.bson.conversions.Bson,options:org.mongodb.scala.model.DeleteOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.DeleteResult])
>
> - [Additional Methods](reference-delete-methods.html#additional-deletes)
>
>