Docs Home → MongoDB for VS Code
Update Documents更新文档
On this page本页内容
You can update documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB Playground中使用MongoDB CRUD 运算符更新集合中的文档:
Use the updateOne() method to update one document.使用updateOne()
方法更新一个文档。Use the updateMany() method to update more than one document.使用updateMany()
方法可以更新多个文档。
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更新文档:
Create a connection to a MongoDB deployment.创建到MongoDB部署的连接。Activate the connection to the MongoDB deployment.激活到MongoDB部署的连接。Open a MongoDB Playground.打开一个MongoDB演练场。Create Documents创建文档or create documents in a collection using a different method.或者使用不同的方法在集合中创建文档。
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. 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实例
The following example:以下示例:
Switches to the切换到test
database.test
数据库。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, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的Playground,并在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,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0,
insertedId: null
}
Update Many Documents更新许多文档
To update many documents, use the following syntax in your 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. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。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实例
The following example:以下示例:
Switches to the切换到test
database.test
数据库。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, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的Playground,并在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,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0,
insertedId: null
}