Docs Home / VS Code Extension / Explore with Playgrounds / Perform CRUD Operations

Delete Documents with VS Code删除带有VS Code的文档

You delete documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB游乐场中使用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游乐场删除文档:

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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.要运行游乐场,请按游乐场视图右上角的“播放”按钮。VS Code扩展拆分游乐场,并在“游乐场Results.json”窗格中输出游乐场的结果。如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。

Example示例

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

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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在游乐场结果json窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。如果手动移动游乐场结果,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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. 要运行游乐场,请按游乐场视图右上角的“播放”按钮。VS Code扩展拆分游乐场,并在游乐场Results.json窗格中输出游乐场的结果。If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。

Example示例

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

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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下播放按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。如果手动移动游乐场结果,VS Code延伸将在该选项卡中显示结果。

{
acknowleged: 1,
deletedCount: 3
}