Docs Menu
Docs Home
/
MongoDB for VS Code
/ /

Update Documents with VS Code

On this page

  • Prerequisites
  • Update One Document
  • Update Many Documents

You can update documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:

  • Use the updateOne() method to update one document.

  • Use the updateMany() method to update more than one document.

If you have not done so already, you must complete the following prerequisites before you can update documents with a MongoDB Playground:

  • Create a connection to a MongoDB deployment.

  • Activate the connection to the MongoDB deployment.

  • Open a MongoDB Playground.

  • Create Documents with VS Code or create documents in a collection using a different method.

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.

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.

The following example:

  1. Switches to the test database.

  2. Updates one document in the test.sales collection that matches the filter.

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. 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.

{
acknowleged: 1,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0,
insertedId: null
}

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.

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.

The following example:

  1. Switches to the test database.

  2. Updates all documents in the test.sales collection that match the filter.

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. 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.

{
acknowleged: 1,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0,
insertedId: null
}

Back

Read