Try Studio 3T for Free

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 be made easily in a few clicks.

Throughout this article, we’ll be updating the customers collection. Feel free to  download it here to follow along the examples.

Update a single MongoDB document

db.collection.updateOne() updates a single document that matches the specified query criteria.

Example

There is a customer named Phylis Grey in our customers collection. Her last name was misspelled and needs to be corrected to Gray.

Update via mongo shell or IntelliShell

In the mongo shell or on IntelliShell, we would run the following updateOne() command:

db.customers.updateOne( 
   { "_id" : ObjectId("57d28452ed5d4d54e8687078") },
   { $set: { "last" : "Gray" }
  }
)

If successful, you will see matchedCount = 1 and modifiedCount = 1:

MongoDB update: single documents in the mongo shell

Update via Tree View and Table View

You can do the same update in Tree View and Table View using In-Place Data Editing. All you need to do is double-click on any cell to edit its value.

  1. Build a query through the Visual Query Builder or type a query in the Main Query Bar.
  2. Run the query.
  3. Locate the target document – in this case, our customer Phylis Grey – and double-click on the target field last to edit its value.MongoDB update via In-Place Editing
  4. Type the new value and press Enter.

Update 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 document: JSON View

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

Update multiple MongoDB documents (e.g. matching the query criteria)

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

Example

The XXL package has been discontinued, so all 197 customers with this package need to be moved to the Premium package.

197 customers whose package need to be updated

Update via mongo shell or IntelliShell

We can perform the bulk update by running the updateMany() command:

db.customers.updateMany( 
    { "package" : "XXL" }, 
    { $set: { "package" : "Premium" } 
  } 
)

In one go, the query successfully updated matched and modified 197 documents.

MongoDB update: 197 documents were matched and modified

Update via Tree View and Table View

  1. Build the query to get to the 197 customers through the Visual Query Builder (drag the field package into the builder and type XXL as value), or type the query { "package": "XXL" }  in the Main Query Bar.
  2. Run the query.
  3. Right-click on the target field, which in this case is package.
  4. Choose Field > Edit Value/Type.
  5. Update the value from XXL to Premium.
  6. 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”.

    In this example, we will choose “Documents matching query criteria“.

    Update MongoDB documents matching query criteria

  7. Click Set Value.

To update all documents in a MongoDB collection, you would instead need to choose All documents in collection.

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 September 13, 2019

Was this article helpful?

Related Articles

Leave a Comment