Docs Menu

Docs HomeDevelop ApplicationsMongoDB for VS Code

Update Documents

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:

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. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code 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, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 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.

{
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. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code 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, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 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.

{
acknowleged: 1,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0,
insertedId: null
}
←  Read DocumentsDelete Documents →