How to Update MongoDB Documents

There are various MongoDB update methods for updating documents in a collection, whether it’s for one document, multiple, or all documents.

In Studio 3T, the In-Place Data Editing feature takes care of most updates to a single document. Updates to multiple or all documents can as easily be made in a few clicks.

In-Place Data Editing is available in Tree View and Table View. Real-time updates can also be made in JSON View through the built-in Document Editor.

Update a single MongoDB document

db.collection.updateOne() updates a single document that matches the specified query criteria, even though multiple documents match the criteria.

Via mongo shell (e.g. IntelliShell)

Run the following command:

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

Via Tree View and Table View

  1. Build a query through the Visual Query Builder, or type a query in the Main Query Bar and run it.
  2. Locate the target document and double-click on the target field to edit its value.MongoDB update via In-Place Editing
  3. Type the new value and press Enter.

Via JSON View

  1. Right-click on the target document.
  2. Choose Edit document.
  3. Find the target field and type in the new value.MongoDB update in JSON view

4. Validate the code (if needed) and click Update.

Update all MongoDB documents (or multiple documents matching the query criteria)

db.collection.updateMany() updates all documents within a collection that match the query criteria.

Via mongo shell (e.g. IntelliShell)

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

Via Tree View and Table View

  1. Build a query through the Visual Query Builder, or type a query in the Main Query Bar and run it.
  2. Right-click on the target field.
  3. Choose Field > Edit Value/Type.
  4. Update the value.
  5. Choose to set the fields in either “Documents matching query criteria”, “Documents matching query criteria that have this field”, “All documents in collection”, or “All Documents in collection that have this field”.MongoDB update methods on Studio 3T
  6. Click Set Value.

Complement this short read with another on how MongoDB insert() works in Studio 3T, or how to rename MongoDB fields without typing a single $rename operator.

Updated on May 27, 2019

Was this article helpful?

Related Articles

Leave a Comment