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

Update Documents with VS Code使用VS Code更新文档

You can update 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 update documents with a MongoDB Playground:如果您还没有这样做,则必须先完成以下先决条件,然后才能使用MongoDB Playground更新文档:

Update One Document更新一个文档

To update one document, use the following syntax in your Playground:要更新一个文档,请在游乐场中使用以下语法:

db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
}
)

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

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示例

The following example:以下示例:

  1. Switches to the test database.切换到test数据库。
  2. Updates one document in the test.sales collection that matches the filter.更新test.sales集合中与筛选器匹配的一个文档。
 use("test");

db.sales.updateOne(
{ "_id" : 1},
{ $inc: { "quantity" : 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扩展会拆分游乐场,并在“游乐场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,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0,
insertedId: null
}

Update Many Documents更新许多文档

To update many documents, use the following syntax in your Playground:要更新许多文档,请在Playground中使用以下语法:

db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
}
)

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

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示例

The following example:以下示例:

  1. Switches to the test database.切换到test数据库。
  2. Updates all documents in the test.sales collection that match the filter.更新test.sales集合中与筛选器匹配的所有文档。
use("test");

db.sales.updateMany(
{ "item" : "abc" },
{ $set: { "price": 9 }}
);

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,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0,
insertedId: null
}