You can delete documents in MongoDB using the following methods:您可以使用以下方法在MongoDB中删除文档:
Your programming language's driver.编程语言的驱动程序。The MongoDB Atlas UI. To learn more, see Delete a Document with MongoDB Atlas.MongoDB Atlas用户界面。要了解更多信息,请参阅使用MongoDB Atlas删除文档。- MongoDB Compass.
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.使用右上角的“选择语言”下拉菜单设置以下示例的语言,或选择MongoDB Compass。
MongoDB Shell
This page uses the following 此页面使用以下mongosh methods:mongosh方法:
The examples on this page use the 此页面上的示例使用inventory collection. To populate the inventory collection, run the following:inventory集合。要填充inventory集合,请运行以下命令:
Compass
This page uses MongoDB Compass to delete the documents.此页面使用MongoDB Compass删除文档。
Populate the 用以下文档填充inventory collection with the following documents:inventory集合:
C
This page uses the following MongoDB C Driver methods:
- mongoc_collection_delete_one此页面使用以下MongoDB C驱动程序方法:mongoc_collection_delete_one
mongoc_collection_delete_many
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
C#
This page uses the following MongoDB C# Driver methods:此页面使用以下MongoDB C#驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Go
This page uses the following MongoDB Go Driver functions:此页面使用以下MongoDB Go驱动程序函数:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Java(Async)
This page uses the following Java Reactive Streams Driver methods:此页面使用以下Java Reactive Streams驱动程序方法:
com.mongodb.reactivestreams.client.MongoCollection.deleteManycom.mongodb.reactivestreams.client.MongoCollection.deleteOne
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Java(Sync)
This page uses the following Java Synchronous Driver methods:此页面使用以下Java同步驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Kotlin(Coroutine)
This page uses the following Kotlin Coroutine Driver methods:此页面使用以下Kotlin协程驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Motor
This page uses the following Motor driver methods:此页面使用以下Motor驱动器方法:
motor.motor_asyncio.AsyncIOMotorCollection.delete_manymotor.motor_asyncio.AsyncIOMotorCollection.delete_one
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Node.js
This page uses the following MongoDB Node.js Driver methods:此页面使用以下MongoDB Node.js驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
PHP
This page uses the following MongoDB PHP Library methods:此页面使用以下MongoDB PHP库方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Python
This page uses the following PyMongo Python driver methods:此页面使用以下PyMongo Python驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
Ruby
This page uses the following MongoDB Ruby Driver methods:此页面使用以下MongoDB Ruby驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
scala
This page uses the following MongoDB Scala Driver methods:此页面使用以下MongoDB Scala驱动程序方法:
The examples on this page use the 此页面上的示例使用inventory collection. Connect to a test database in your MongoDB instance then create the inventory collection:inventory集合。连接到MongoDB实例中的测试数据库,然后创建inventory集合:
MongoDB Shell
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" },
] );Compass
[
{ "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" }
]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.有关在MongoDB Compass中插入文档的说明,请参阅插入文档。
Note
For complete reference on inserting documents in MongoDB Compass, see the Compass documentation.有关在MongoDB Compass中插入文档的完整参考,请参阅Compass文档。
C
mongoc_collection_t *collection;
mongoc_bulk_operation_t *bulk;
bson_t *doc;
bool r;
bson_error_t error;
bson_t reply;
collection = mongoc_database_get_collection (db, "inventory");
bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
doc = BCON_NEW (
"item", BCON_UTF8 ("journal"),
"qty", BCON_INT64 (25),
"size", "{",
"h", BCON_DOUBLE (14),
"w", BCON_DOUBLE (21),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("A"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("notebook"),
"qty", BCON_INT64 (50),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"status", BCON_UTF8 ("P"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("paper"),
"qty", BCON_INT64 (100),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"status", BCON_UTF8 ("D"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("planner"),
"qty", BCON_INT64 (75),
"size", "{",
"h", BCON_DOUBLE (22.85),
"w", BCON_DOUBLE (30),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("D"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("postcard"),
"qty", BCON_INT64 (45),
"size", "{",
"h", BCON_DOUBLE (10),
"w", BCON_DOUBLE (15.25),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("A"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
/* "reply" is initialized on success or error */
r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
}C#
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);Go
docs := []any{
bson.D{
{"item", "journal"},
{"qty", 25},
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
{"status", "A"},
},
bson.D{
{"item", "notebook"},
{"qty", 50},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"status", "P"},
},
bson.D{
{"item", "paper"},
{"qty", 100},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"status", "D"},
},
bson.D{
{"item", "planner"},
{"qty", 75},
{"size", bson.D{
{"h", 22.85},
{"w", 30},
{"uom", "cm"},
}},
{"status", "D"},
},
bson.D{
{"item", "postcard"},
{"qty", 45},
{"size", bson.D{
{"h", 10},
{"w", 15.25},
{"uom", "cm"},
}},
{"status", "A"},
},
}
result, err := coll.InsertMany(context.TODO(), docs)Java(Async)
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' }")
));Java(Sync)
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' }")
));Kotlin(Coroutine)
collection.insertMany(
listOf(
Document("item", "journal")
.append("qty", 25)
.append("size", Document("h", 14).append("w", 21).append("uom", "cm"))
.append("status", "A"),
Document("item", "notebook")
.append("qty", 50)
.append("size", Document("h", 8.5).append("w", 11).append("uom", "in"))
.append("status", "A"),
Document("item", "paper")
.append("qty", 100)
.append("size", Document("h", 8.5).append("w", 11).append("uom", "in"))
.append("status", "D"),
Document("item", "planner")
.append("qty", 75)
.append("size", Document("h", 22.85).append("w", 30).append("uom", "cm"))
.append("status", "D"),
Document("item", "postcard")
.append("qty", 45)
.append("size", Document("h", 10).append("w", 15.25).append("uom", "cm"))
.append("status", "A"),
)
)Motor
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",
},
]
)Node.js
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'
}
]);PHP
$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',
],
]);Python
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",
},
]
)Ruby
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' },
])scala
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()Delete All Documents删除所有文档
MongoDB Shell
To delete all documents from a collection, pass an empty filter document 要从集合中删除所有文档,请将一个空的筛选文档{} to the db.collection.deleteMany() method.{}传递给db.collection.deleteMany()方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
C
To delete all documents from a collection, pass the mongoc_collection_t and a bson_t that matches all documents to the mongoc_collection_delete_many method.要从集合中删除所有文档,请将mongoc_collection_t和匹配所有文档的bson_t传递给mongoc_collection_delete_many方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
C#
To delete all documents from a collection, pass an empty filter 要从集合中删除所有文档,请将空筛选器Builders<BsonDocument>.Filter.Empty to the IMongoCollection.DeleteMany() method.Builders<BsonDocument>.Filter.Empty传递给IMongoCollection.DeleteMany()方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Go
To delete all documents from a collection, pass an empty filter document to the Collection.DeleteMany function.要从集合中删除所有文档,请将一个空的筛选文档传递给Collection.DeleteMany函数。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Java(Async)
To delete all documents from a collection, pass an empty org.bson.Document object as the filter to the com.mongodb.reactivestreams.client.MongoCollection.deleteMany method.要从集合中删除所有文档,请将一个空的org.bson.Document对象作为筛选器传递给com.mongodb.reactivestreams.client.MongoCollection.deleteMany方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Java(Sync)
To delete all documents from a collection, pass an empty org.bson.Document object as the filter to the com.mongodb.client.MongoCollection.deleteMany method.要从集合中删除所有文档,请将空的org.bson.Document对象作为筛选器传递给com.mongodb.client.MongoCollection.deleteMany方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Kotlin(Coroutine)
To delete all documents from a collection, pass an empty 要从集合中删除所有文档,请将一个空的Bson object as the filter to the MongoCollection.deleteMany() method.Bson对象作为筛选器传递给MongoCollection.deleteMany()方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Motor
To delete all documents from a collection, pass an empty filter document 要从集合中删除所有文档,请将空的筛选文档{} to the motor.motor_asyncio.AsyncIOMotorCollection.delete_many method.{}传递给motor.motor_asyncio.AsyncIOMotorCollection.delete_many方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Node.js
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集合中删除所有文档:
PHP
To delete all documents from a collection, pass an empty filter document 要从集合中删除所有文档,请将一个空的筛选器文档[] to the MongoDB\\Collection::deleteMany() method.[]传递给MongoDB\\Collection::deleteMany()方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Python
To delete all documents from a collection, pass an empty filter document 要从集合中删除所有文档,请将空的筛选文档{} to the pymongo.collection.Collection.delete_many method.{}传递给pymongo.collection.Collection.delete_many方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
Ruby
To delete all documents from a collection, pass an empty filter document 要从集合中删除所有文档,请将一个空的筛选文档{} to the Mongo::Collection#delete_many() method.{}传递给Mongo::Collection#delete_many()方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
scala
To delete all documents from a collection, pass an empty filter 要从集合中删除所有文档,请将空筛选器Document() to the collection.deleteMany() method.Document()传递给collection.deleteMany()方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
MongoDB Shell
db.inventory.deleteMany({})C
mongoc_collection_t *collection;
bson_t *selector;
bool r;
bson_error_t error;
collection = mongoc_database_get_collection (db, "inventory");
selector = BCON_NEW (NULL);
r = mongoc_collection_delete_many (collection, selector, NULL, NULL, &error);
bson_destroy (selector);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}C#
var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.DeleteMany(filter);Go
result, err := coll.DeleteMany(context.TODO(), bson.D{})Java(Async)
Publisher<DeleteResult> deleteManyPublisher = collection.deleteMany(new Document());Java(Sync)
collection.deleteMany(new Document());Kotlin(Coroutine)
collection.deleteMany(empty())Motor
await db.inventory.delete_many({})Node.js
await db.collection('inventory').deleteMany({});PHP
$deleteResult = $db->inventory->deleteMany([]);Python
db.inventory.delete_many({})Ruby
client[:inventory].delete_many({})scala
collection.deleteMany(Document()).execute()MongoDB Shell
The method returns a document with the status of the operation. For more information and examples, see 该方法返回一个包含操作状态的文档。有关更多信息和示例,请参阅deleteMany().deleteMany()。
C
The mongoc_collection_delete_many method returns true if successful, or returns false and sets an error if there are invalid arguments or a server or network error occurs.mongoc_collection_delete_many方法如果成功则返回true,如果存在无效参数或发生服务器或网络错误,则返回false并设置错误。
C#
Upon successful execution, the 成功执行后,IMongoCollection.DeleteMany() method returns an instance of DeleteResult whose DeletedCount property contains the number of documents that matched the filter.IMongoCollection.DeleteMany()方法返回一个DeleteResult实例,其DeletedCount属性包含与筛选器匹配的文档数量。
Go
Upon successful execution, the Collection.DeleteMany function returns an instance of DeleteResult whose 成功执行后,DeletedCount property contains the number of documents that matched the filter.Collection.DeleteMany函数返回一个DeleteResult实例,其DeletedCount属性包含与筛选器匹配的文档数量。
Java(Async)
com.mongodb.reactivestreams.client.MongoCollection.deleteMany returns a Publisher object of type com.mongodb.client.result.DeleteResult if successful. 如果成功,com.mongodb.reactivestreams.client.MongoCollection.deleteMany将返回com.mongodb.client.result.DeleteResult类型的Publisher对象。Returns an instance of 如果失败,则返回com.mongodb.MongoException if unsuccessful.com.mongodb.MongoException的实例。
Java(Sync)
The com.mongodb.client.MongoCollection.deleteMany method returns an instance of com.mongodb.client.result.DeleteResult with the status of the operation.com.mongodb.client.MongoCollection.deleteMany方法返回一个包含操作状态的com.mongodb.client.result.DeleteResult实例。
Kotlin(Coroutine)
The MongoCollection.deleteMany() method returns an instance of com.mongodb.client.result.DeleteResult that describes the status of the operation and count of deleted documents.MongoCollection.deleteMany()方法返回一个com.mongodb.client.result.DeleteResult实例,该实例描述了操作状态和已删除文档的计数。
Motor
The delete_many coroutine asynchronously returns an instance of pymongo.results.DeleteResult with the status of the operation.delete_many协程异步返回pymongo.results.DeleteResult的一个实例,其中包含操作的状态。
Node.js
Collection.deleteMany() returns a promise that provides a 返回一个提供result. The result.deletedCount property contains the number of documents that matched the filter.result的promise。result.deletedCount属性包含与筛选器匹配的文档数。
PHP
Upon successful execution, the 成功执行后,deleteMany() method returns an instance of MongoDB\\DeleteResult whose getDeletedCount() method returns the number of documents that matched the filter.deleteMany()方法返回MongoDB\\DeleteResult的一个实例,其getDeletedCount()方法将返回与筛选器匹配的文档数量。
Python
The delete_many method returns an instance of pymongo.results.DeleteResult with the status of the operation.delete_many方法返回带有操作状态的pymongo.results.DeleteResult实例。
Ruby
Upon successful execution, the delete_many() method returns an instance of Mongo::Operation::Result, whose deleted_count attribute contains the number of documents that matched the filter.
scala
Upon successful execution, the collection.deleteMany() method returns an Observable with a single element with a DeleteResult type parameter or with an com.mongodb.MongoException.
Delete All Documents that Match a Condition删除符合条件的所有文档
MongoDB Shell
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"的所有文档:
C
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 the mongoc_collection_t and a bson_t that matches the documents to be deleted to the mongoc_collection_delete_many method.要删除符合删除条件的所有文档,请将mongoc_collection_t和与要删除的文档匹配的bson_t传递给mongoc_collection_delete_many方法。
C#
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, construct a filter using the Eq method:要指定相等条件,请使用Eq方法构造一个筛选器:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. 除了等式筛选器外,MongoDB还提供了各种查询运算符来指定筛选条件。Use the FilterDefinitionBuilder methods to create a filter document. For example:使用FilterDefinitionBuilder方法创建筛选器文档。例如:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
To delete all documents that match a deletion criteria, pass a filter parameter to the 要删除符合删除条件的所有文档,请将筛选器参数传递给IMongoCollection.DeleteMany() method.IMongoCollection.DeleteMany()方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status字段等于"A"的所有文档:
Go
You can specify criteria, or filters, that identify the documents to delete. The filters use the same syntax as read operations.您可以指定用于标识要删除的文档的条件或筛选器。筛选器使用与读取操作相同的语法。
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the bson package to create query operators for filter documents. For example:除了等式筛选器外,MongoDB还提供了各种查询运算符来指定筛选条件。使用bson包为筛选文档创建查询运算符。例如:
filter := bson.D{
{"$and", bson.A{
bson.D{{"field1", bson.D{{"$eq", value1}}}},
bson.D{{"field2", bson.D{{"$lt", value2}}}},
}},
}
To delete all documents that match a deletion criteria, pass a filter parameter to the Collection.DeleteMany function.要删除符合删除条件的所有文档,请将筛选器参数传递给Collection.DeleteMany函数。
The following example removes all documents from the inventory collection where the status field equals "A":
Java(Async)
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 the com.mongodb.client.model.Filters.eq method to create the query filter document:要指定相等条件,请使用com.mongodb.client.model.Filters.eq方法创建查询筛选器文档:
and(eq(<field1>, <value1>), eq(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. 除了相等条件外,MongoDB还提供了各种查询运算符来指定筛选条件。Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:使用com.mongodb.client.model.Filters辅助方法来简化筛选器文档的创建。例如:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To delete all documents that match a deletion criteria, pass a filter parameter to the com.mongodb.reactivestreams.client.MongoCollection.deleteMany method.要删除符合删除条件的所有文档,请将筛选器参数传递给com.mongodb.reactivestreams.client.MongoCollection.deleteMany方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status字段等于"A"的所有文档:
Java(Sync)
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 the 要指定相等条件,请使用com.mongodb.client.model.Filters.eq_ method to create the query filter document:com.mongodb.client.model.Filters.eq_方法创建查询筛选器文档:
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:除了相等条件外,MongoDB还提供了各种查询运算符来指定筛选条件。使用com.mongodb.client.model.Filters辅助方法来简化筛选器文档的创建。例如:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To delete all documents that match a deletion criteria, pass a filter parameter to the com.mongodb.client.MongoCollection.deleteMany method.要删除符合删除条件的所有文档,请将筛选器参数传递给com.mongodb.client.MongoCollection.deleteMany方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status字段等于"A"的所有文档:
Kotlin(Coroutine)
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 the 要指定相等条件,请使用Filters.eq() method to create the query filter document:Filters.eq()方法创建查询筛选器文档:
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:除了相等条件外,MongoDB还提供了各种查询运算符来指定筛选条件。使用com.mongodb.client.model.Filters辅助方法来简化筛选器文档的创建。例如:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
To delete all documents that match a deletion criteria, pass a filter parameter to the 要删除符合删除条件的所有文档,请将筛选器参数传递给MongoCollection.deleteMany() method.MongoCollection.deleteMany()方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status字段等于"A"的所有文档:
Motor
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 要删除符合删除条件的所有文档,请将筛选器参数传递给delete_many method.delete_many方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status字段等于"A"的所有文档:
Node.js
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"的所有文档:
PHP
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"的所有文档:
Python
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 要删除符合删除条件的所有文档,请将筛选器参数传递给delete_many method.delete_many方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status段等于"A"的所有文档:
Ruby
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 要删除符合删除条件的所有文档,请将筛选器参数传递给delete_many() method.delete_many()方法。
The following example removes all documents from the 以下示例从inventory collection where the status field equals "A":inventory集合中删除status字段等于"A"的所有文档:
scala
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 the 要指定相等条件,请使用com.mongodb.client.model.Filters.eq_ method to create the query filter document:com.mongodb.client.model.Filters.eq_方法创建查询筛选器文档:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the 除了相等条件外,MongoDB还提供了各种查询运算符来指定筛选条件。使用com.mongodb.client.model.Filters_ helper methods to facilitate the creation of filter documents. For example:com.mongodb.client.model.Filters_助手方法来简化筛选器文档的创建。例如:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
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"的所有文档:
MongoDB Shell
db.inventory.deleteMany({ status : "A" })C
mongoc_collection_t *collection;
bson_t *selector;
bool r;
bson_error_t error;
collection = mongoc_database_get_collection (db, "inventory");
selector = BCON_NEW ("status", BCON_UTF8 ("A"));
r = mongoc_collection_delete_many (collection, selector, NULL, NULL, &error);
bson_destroy (selector);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}C#
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var result = collection.DeleteMany(filter);Go
result, err := coll.DeleteMany(
context.TODO(),
bson.D{
{"status", "A"},
},
)Java(Async)
deleteManyPublisher = collection.deleteMany(eq("status", "A"));Java(Sync)
collection.deleteMany(eq("status", "A"));Kotlin(Coroutine)
collection.deleteMany(eq("status", "A"));Motor
await db.inventory.delete_many({"status": "A"})Node.js
await db.collection('inventory').deleteMany({ status: 'A' });PHP
$deleteResult = $db->inventory->deleteMany(['status' => 'A']);Python
db.inventory.delete_many({"status": "A"})Ruby
client[:inventory].delete_many(status: 'A')scala
collection.deleteMany(equal("status", "A")).execute()MongoDB Shell
The method returns a document with the status of the operation. For more information and examples, see 该方法返回一个包含操作状态的文档。有关更多信息和示例,请参阅deleteMany().deleteMany()。
C
The mongoc_collection_delete_many method returns true if successful, or returns false and sets an error if there are invalid arguments or a server or network error occurs.mongoc_collection_delete_many方法如果成功则返回true,如果存在无效参数或发生服务器或网络错误,则返回false并设置错误。
C#
Upon successful execution, the 成功执行后,IMongoCollection.DeleteMany() method returns an instance of DeleteResult whose DeletedCount property contains the number of documents that matched the filter.IMongoCollection.DeleteMany()方法返回一个DeleteResult实例,其DeletedCount属性包含与筛选器匹配的文档数量。
Go
Upon successful execution, the Collection.DeleteMany function returns an instance of DeleteResult whose 成功执行后,DeletedCount property contains the number of documents that matched the filter.Collection.DeleteMany函数返回一个DeleteResult实例,其DeletedCount属性包含与筛选器匹配的文档数量。
Java(Async)
如果成功,com.mongodb.reactivestreams.client.MongoCollection.deleteMany returns a Publisher object of type com.mongodb.client.result.DeleteResult if successful. com.mongodb.reactivestreams.client.MongoCollection.deleteMany将返回com.mongodb.client.result.DeleteResult类型的Publisher对象。Returns an instance of 如果失败,则返回com.mongodb.MongoException if unsuccessful.com.mongodb.MongoException的实例。
Java(Sync)
The com.mongodb.client.MongoCollection.deleteMany method returns an instance of com.mongodb.client.result.DeleteResult with the status of the operation.com.mongodb.client.MongoCollection.deleteMany方法返回一个包含操作状态的com.mongodb.client.result.DeleteResult实例。
Kotlin(Coroutine)
The MongoCollection.deleteMany() method returns an instance of com.mongodb.client.result.DeleteResult that describes the status of the operation and count of deleted documents.MongoCollection.deleteMany()方法返回一个com.mongodb.client.result.DeleteResult实例,该实例描述了操作状态和已删除文档的计数。
Motor
The delete_many coroutine asynchronously returns an instance of pymongo.results.DeleteResult with the status of the operation.delete_many协程异步返回pymongo.results.DeleteResult的一个实例,其中包含操作的状态。
Node.js
Collection.deleteMany() returns a promise that provides a 返回一个提供result. result的promise。The result.deletedCount property contains the number of documents that matched the filter.result.deletedCount属性包含与筛选器匹配的文档数。
PHP
Upon successful execution, the 成功执行后,deleteMany() method returns an instance of MongoDB\\DeleteResult whose getDeletedCount() method returns the number of documents that matched the filter.deleteMany()方法返回MongoDB\\DeleteResult的一个实例,其getDeletedCount()方法将返回与筛选器匹配的文档数量。
Python
The delete_many method returns an instance of pymongo.results.DeleteResult with the status of the operation.delete_many方法返回带有操作状态的pymongo.results.DeleteResult实例。
Ruby
Upon successful execution, the 成功执行后,delete_many() method returns an instance of Mongo::Operation::Result, whose deleted_count attribute contains the number of documents that matched the filter.delete_many()方法返回Mongo::Operation::Result的实例,其deleted_count属性包含与筛选器匹配的文档数量。
scala
Upon successful execution, the 成功执行后,collection.deleteMany() method returns an Observable with a single element with a DeleteResult type parameter or with an com.mongodb.MongoException.collection.deleteMany()方法返回一个Observable,其中包含一个具有DeleteResult类型参数或com.mongodb.MongoException的元素。
Delete Only One Document that Matches a Condition仅删除一个符合条件的文档
MongoDB Shell
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()方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Compass
MongoDB Compass provides a simple way to delete a document from a collection. The following example shows how to delete the document with MongoDB Compass提供了一种从集合中删除文档的简单方法。以下示例显示了如何从item equal to paper from the inventory collection:inventory集合中删除item等于paper的文档:
Note
In this example we are using the Compass Table View to delete the document. 在这个例子中,我们使用Compass表视图删除文档。The deletion process using the Compass List View follows a very similar approach.使用Compass列表视图的删除过程遵循非常相似的方法。
For more information on the differences between Table View and List View in Compass, refer to the Compass documentation.有关Compass中表视图和列表视图之间差异的更多信息,请参阅Compass文档。
C
To delete a single document from a collection, pass the mongoc_collection_t and a bson_t that matches the document you want to delete to the mongoc_collection_delete_one method.要从集合中删除单个文档,请将mongoc_collection_t和与要删除的文档匹配的bson_t传递给mongoc_collection_delete_one方法。
The following example deletes all documents from the 以下示例从inventory collection:inventory集合中删除所有文档:
C#
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the 要删除最多一个符合指定筛选器的文档(即使多个文档可能符合指定筛选器),请使用IMongoCollection.DeleteOne() method.IMongoCollection.DeleteOne()方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Go
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 function.要删除最多一个符合指定筛选器的文档(即使多个文档可能符合指定筛选器),请使用Collection.DeleteOne函数。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Java(Async)
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the com.mongodb.reactivestreams.client.MongoCollection.deleteMany method.要删除最多一个符合指定筛选器的文档(即使多个文档可能符合指定筛选器),请使用com.mongodb.reactivestreams.client.MongoCollection.deleteMany方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Java(Sync)
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the com.mongodb.client.MongoCollection.deleteOne method.要删除最多一个符合指定筛选器的文档(即使多个文档可能符合指定筛选器),请使用com.mongodb.client.MongoCollection.deleteOne方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Kotlin(Coroutine)
To delete at most a single document that matches a specified filter, even if multiple documents match the specified filter, you can use the 要删除最多一个符合指定筛选器的文档,即使有多个文档符合指定筛选器,也可以使用MongoCollection.deleteOne() method.MongoCollection.deleteOne()方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Motor
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the 要删除最多一个与指定筛选器匹配的文档(即使多个文档可能与指定的筛选器匹配),请使用motor.motor_asyncio.AsyncIOMotorCollection.delete_one method.motor.motor_asyncio.AsyncIOMotorCollection.delete_one方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Node.js
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"的第一个文档:
PHP
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the 要删除最多一个与指定筛选器匹配的文档(即使多个文档可能与指定的筛选器匹配),请使用MongoDB\\Collection::deleteOne() method.MongoDB\\Collection::deleteOne()方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Python
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the 要删除最多一个符合指定筛选器的文档(即使多个文档可能符合指定筛选器),请使用pymongo.collection.Collection.delete_one method.pymongo.collection.Collection.delete_one方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
Ruby
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the Mongo::Collection#delete_one() method.要删除最多一个符合指定筛选器的文档(即使多个文档可能符合指定筛选器),请使用Mongo::Collection#delete_one()方法。
The following example deletes the first document where 以下示例删除status is "D":status为"D"的第一个文档:
scala
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"的第一个文档:
MongoDB Shell
db.inventory.deleteOne( { status: "D" } )Compass
Click the Table button in the top navigation to access the Table View:单击顶部导航中的“表格”按钮以访问“表格视图”:
Use the Compass query bar to locate the target document.使用Compass“查询栏”定位目标文档。Copy the following filter document into the query bar and click Find:将以下筛选文档复制到查询栏中,然后单击“查找”:{ item: "paper" }
Hover over the document and click the trash icon which appears on the right-hand side:将鼠标悬停在文档上,然后单击右侧出现的垃圾图标:
After clicking the delete button, the document is flagged for deletion and Compass asks for confirmation that you want to remove the document:单击删除按钮后,文档将被标记为删除,Compass会要求您确认是否要删除文档:
Click Delete to confirm. Compass deletes the document from the collection.单击“删除”进行确认。Compass从集合中删除文档。
C
mongoc_collection_t *collection;
bson_t *selector;
bool r;
bson_error_t error;
collection = mongoc_database_get_collection (db, "inventory");
selector = BCON_NEW ("status", BCON_UTF8 ("D"));
r = mongoc_collection_delete_one (collection, selector, NULL, NULL, &error);
bson_destroy (selector);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
Be sure to also clean up any open resources by calling the following methods, as appropriate:请确保根据需要调用以下方法来清理任何开放资源:
C#
var filter = Builders<BsonDocument>.Filter.Eq("status", "D");
var result = collection.DeleteOne(filter);Go
result, err := coll.DeleteOne(
context.TODO(),
bson.D{
{"status", "D"},
},
)Java(Async)
Publisher<DeleteResult> deleteOnePublisher = collection.deleteOne(eq("status", "D"));Java(Sync)
collection.deleteOne(eq("status", "D"));Motor
await db.inventory.delete_one({"status": "D"})Node.js
await db.collection('inventory').deleteOne({ status: 'D' });PHP
$deleteResult = $db->inventory->deleteOne(['status' => 'D']);Python
db.inventory.delete_one({"status": "D"})Ruby
client[:inventory].delete_one(status: 'D')scala
collection.deleteOne(equal("status", "D")).execute()Delete a Document with MongoDB Atlas使用MongoDB Atlas删除文档
Note
You can delete only one document at a time in the MongoDB Atlas UI. 在MongoDB Atlas UI中,一次只能删除一个文档。To delete multiple documents, connect to your Atlas deployment from 要删除多个文档,请从mongosh or a MongoDB driver and follow the examples on this page for your preferred method.mongosh或MongoDB驱动程序连接到Atlas部署,并按照本页上的示例选择首选方法。
The example in this section uses the sample movies dataset. To learn how to load the sample dataset into your MongoDB Atlas deployment, see Load Sample Data.本节中的示例使用示例电影数据集。要了解如何将示例数据集加载到MongoDB Atlas部署中,请参阅加载示例数据。
To delete a document in MongoDB Atlas, follow these steps:要删除MongoDB Atlas中的文档,请执行以下步骤:
In the MongoDB Atlas UI, go to the Clusters page for your project.在MongoDB Atlas UI中,转到项目的“集群”页面。
Warning
Navigation Improvements In Progress导航改进正在进行中
We're currently rolling out a new and improved navigation experience. If the following steps don't match your view in the Atlas UI, see the preview Atlas documentation.我们目前正在推出一种新的、改进的导航体验。如果以下步骤与Atlas UI中的视图不匹配,请参阅Atlas预览文档。
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.如果尚未显示,请从导航栏的“组织”菜单中选择包含所需项目的组织。If it's not already displayed, select your project from the Projects menu in the navigation bar.如果尚未显示,请从导航栏中的“项目”菜单中选择项目。If it's not already displayed, click Clusters in the sidebar.如果尚未显示,请单击侧栏中的“集群”。
Navigate to the collection.导航到集合。
For the cluster that contains the sample data, click Browse Collections.对于包含示例数据的集群,单击“浏览集合”。In the left navigation pane, select the在左侧导航窗格中,选择sample_mflixdatabase.sample_mflix数据库。Select the选择moviescollection.movies集合。
Specify a query filter document.指定查询筛选器文档。
Optionally, you can specify a query filter document in the Filter field. A query filter document uses query operators to specify search conditions.您可以在“筛选器”字段中指定查询筛选器文档(可选)。查询筛选器文档使用查询运算符指定搜索条件。
Copy the following query filter document into the Filter search bar and click Apply:将以下查询筛选器文档复制到筛选器搜索栏中,然后单击应用:
{ genres: "Action", rated: { $in: [ "PG", "PG-13" ] } }
This query filter returns all documents in the 此查询筛选器返回sample_mflix.movies collection where genres equals Action and rated equals either PG or PG-13.sample_mflix.movies集合中的所有文档,其中genres等于Action,rated等于PG或PG-13。
Delete a document.删除文档。
For the document that you want to delete, hover over the document and click the trash icon that appears on the right-hand side.对于要删除的文档,将鼠标悬停在文档上,然后单击右侧显示的回收站图标。After clicking the delete button, MongoDB Atlas flags the document for deletion and asks for your confirmation.点击删除按钮后,MongoDB Atlas会将文档标记为删除,并要求您确认。Click Delete to confirm your selection.单击“删除”以确认选择。
To learn more, see Create, View, Update, and Delete Documents.要了解更多信息,请参阅创建、查看、更新和删除文档。
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. For more information on MongoDB and atomicity, see Atomicity and Transactions.MongoDB中的所有写入操作都是单个文档级别的原子操作。有关MongoDB和原子性的更多信息,请参阅原子性和事务。
Write Acknowledgement写入确认
With write concerns, you can specify the level of acknowledgment requested from MongoDB for write operations. For details, see Write Concern.对于写入关注,您可以指定MongoDB为写操作请求的确认级别。有关详细信息,请参阅写入关注。