Docs HomeMongoDB for VS Code

Delete Documents删除文档

You delete documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB Playground中使用MongoDB CRUD运算符删除集合中的文档:

Prerequisites先决条件

If you have not done so already, you must complete the following prerequisites before you can delete documents with a MongoDB Playground:如果您还没有这样做,您必须完成以下先决条件,然后才能使用MongoDB Playground删除文档:

Delete One Document删除一个文档

To delete one document, use the following syntax in your Playground:要删除一个文档,请在演练场中使用以下语法:

db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)

For a detailed description of this method's parameters, see deleteOne() in the MongoDB Manual.有关此方法参数的详细描述,请参阅MongoDB手册中的deleteOne()

To run your Playground, press the Play Button at the top right of the Playground View. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code拆分您的演练场,并在演练场结果json窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出您的演练场的结果。

Example实例

To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。

The following example:以下示例:

  1. Switches to the test database.切换到test数据库。
  2. Deletes one document in the test.sales collection that matches the query.删除test.sales集合中与查询匹配的一个文档。
use("test");

db.sales.deleteOne(
{ "_id" : 1 }
);

When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下播放按钮时,MongoDB for VS Code会拆分您的演练场,并在Playground Results.json窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. If you manually move your playground results, MongoDB for VS Code displays the results in that tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档。如果您手动移动演练场结果,MongoDB of VS Code会显示该选项卡中的结果。

{
acknowleged: 1,
deletedCount: 1
}

Delete Many Documents删除多个文档

To delete many documents, use the following syntax in your Playground:要删除多个文档,请在演练场中使用以下语法:

db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)

For a detailed description of this method's parameters, see deleteMany() in the MongoDB Manual.有关此方法参数的详细描述,请参阅MongoDB手册中的deleteMany()

To run your Playground, press the Play Button at the top right of the Playground View. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code拆分您的演练场,并在Playground Results.json窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出您的演练场的结果。

Example实例

To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。

The following example:以下示例:

  1. Switches to the test database.切换到test数据库。
  2. Deletes all documents in the test.sales collection that match the query.删除test.sales集合中与查询匹配的所有文档。
use("test");

db.sales.deleteMany(
{ "item" : "abc" }
);

When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的演练场,并在Playground Results.json窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. 如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档。If you manually move your playground results, MongoDB for VS Code displays the results in that tab.如果手动移动演练场结果,MongoDB for VS Code会在该选项卡中显示结果。

{
acknowleged: 1,
deletedCount: 3
}