Try Studio 3T for Free
  1. Knowledge Base
  2. MongoDB Tutorials
  3. How to Insert & Update MongoDB Documents

How to Insert & Update MongoDB Documents

A document is the basic unit of data in MongoDB. It’s comprised of field/value pairs which are written in BSON format.

A field is the name/value pair in a MongoDB document while a value is the specific value of a document’s field/value pair. A group of documents together is known as a collection.

Fields in a MongoDB document can be of any BSON data type. The full list of types can be found here.

The most important thing to remember is that each MongoDB document requires a unique _id field, which serves as the primary locator key for each document.

If the user doesn’t specify an _id field when creating a new document, MongoDB automatically creates ObjectId value represented as a string of numbers. This is shown in the examples below:

ObjectId (default)

     _id: ObjectId("57d28452ed5d4d54e8687098"),
     first: "Hugh",
     last: "Manatee",
     email: "[email protected]",

The _id field is immutable, however you can add a new document with an _id field of any BSON data type other than array.

An example of a custom ID (in this example, string)

     _id: "Studio3TMascot",
     first: "Hugh",
     last: "Manatee",
     email: "[email protected]",

The maximum document size is 16 MB, however MongoDB uses GridFS to accommodate larger documents.

In the following examples, we’ll show you how to run document-related CRUD commands through Studio 3T.

Download the latest version of Studio 3T here.

Insert MongoDB documents to a collection

Right-click on any cell while viewing a collection in TableTree, or JSON View.and choose Insert Document. Alternatively, use the hotkey Ctrl + D (⌘ + D).

Right-click on any cell to insert a document

This will open the Insert Document > JSON window.

Type the fields to be added in JSON format. There is no need to add an _id field, as mongod will create this and assign the document a unique ObjectId value.

Insert JSON document to be added

Click on Validate JSON to ensure everything is correct before adding the document.

Click on Add Document, or Add & Continue if adding more documents. The new document should then appear in the collection’s Result tab.The newly added document will then appear in the Result Tab

Update MongoDB documents (updateone, updatemany)

Depending on which view, Studio 3T offers convenient ways to update a single, multiple, or all documents in a collection.

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.

Update a single document

With Table and Tree View, all you need to do is double-click on any cell to edit its value.

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

Type the new value and press Enter.

With JSON View, right-click on the target document and choose Edit document.

Find the target field and type in the new value. Validate the code (if needed) and click Update.

MongoDB update document: JSON View

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

In the example below, the XXL package has been discontinued, so we’ve built the query (through the Visual Query Builder) that gives us the list of 197 customers who need to be moved to the Premium package.

197 customers whose package need to be updated

In Table or Tree View, right-click on the target field, which in this case is package.

Choose Field > Edit Value/Type. Update the value from XXL to Premium.

Choose to set the fields in either:

  • Documents matching query criteria
  • Documents matching query criteria that have this field
  • All documents in collection
  • All Documents in collection that have this field

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

Update MongoDB documents matching query criteria

Click Set Value.

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

Delete MongoDB documents

While in Tree, Table, or JSON View, right-click on the document to delete and choose Remove document or press Shift + Fn + Backspace.

Delete MongoDB documents

Rename MongoDB fields

Every now and then, you may need to rename MongoDB fields.

Whether you need to do it for a single document, or perhaps for multiple documents, this task can be easily accomplished with only a few steps from within Studio 3T.

Rename a field in a single document

The simplest of these tasks is changing a field name in single document. See the steps below:

By editing a document via JSON, you can quickly rename fields. Just double click the field to be changed and begin typing.

  1. Open the document in which the field to be renamed is located.
  2. Double click on the field name (In this case “dob”).
  3. You will now have the option to type the new field name into this space.
  4. Once you press “Enter” the new field name will be saved.

Rename fields within documents matching a query

Should you wish to change all documents matching a particular query then this can also be easily achieved in just a few steps. In this example we are changing the “dob” field in all documents relating to customers with the last name “Miller”. See below:

Right-click and go to 'Rename Field', then choose 'Documents matching query criteria'. This lets you update the field name for all results matching query criteria.

  1. Type in your query: {last:"Miller"} – and hit “Enter”.
  2. Open one of the documents resulting from the query search.
  3. Find the field that you would like to rename (“dob”) and right click it.
  4. Select “Rename Field”.
  5. Type in the new field name (“date_of_birth”) and hit “Enter”.
  6. Select “Documents matching query criteria” from the dropdown box.
  7. Click “Enter”.
  8. Now every document within this query will have the field name “date_of_birth” in place of “dob”.

Rename fields in all documents within a collection

Finally, to rename a field in all documents within a collection follow the steps below (identical to the steps above apart from no. 4):

Right click a field and go to 'Rename Field'. From the drop down menu, rename the field and from the drop-down menu choose 'All documents in collection'. This will then update the field name for all documents in the collection.

  1. Find the field that you would like to rename (again “dob”) and right click it.
  2. Select “Rename Field”.
  3. Type in the new field name (“date_of_birth”) and hit “Enter”.
  4. Select “All documents in collection” from the dropdown box.
  5. Click “Rename”.
  6. Now every document in the entire collection will have the field name “date_of_birth” in place of “dob”.

Check out other ways you can edit documents quickly and easily in Studio 3T:

Updated on September 23, 2020

Was this article helpful?