Update 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.
Prerequisites
If you have not done so already, you must complete the following prerequisites before you can update documents with a MongoDB Playground:
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.
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.
Example
The following example:
Switches to the
test
database.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 }
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.
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.
Example
The following example:
Switches to the
test
database.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 }