Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Reports
    • Case Studies
    • Whitepapers
    • Blog
    • Testimonials
    • Community
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Career
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • My License
  • Download
search

Studio 3T Knowledge Base

  • Documentation
  • Tutorials
  • Workshops
Take the fastest route to learning MongoDB. Cover the basics in two hours with MongoDB 101, no registration required.
Start the free course

How to Insert & Update MongoDB Documents

Posted on: 14/02/2020 (last updated: 04/08/2021) by Thomas Zahn

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 Table, Tree, or JSON View.and choose Insert Document. Alternatively, use the shortcut Ctrl + D (⌘ + D).

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.

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.

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.

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.

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.

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.

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.

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:

  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:

  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):

  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:

  • Edit data in-place while in Table View and Tree View
  • Validate JSON while editing documents in JSON View
  • Find data anomalies like misspellings and duplicates with Schema Explorer (then edit them in-place)

How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Thomas Zahn

Having grown up with a living room that was essentially the office of his mother’s software start-up in the 80s, Thomas is a dyed-in-the-wool software engineer. In the past, he has worked for large outfits such as Microsoft Research and Nokia as well as for specialised engineering shops and start-ups. He lives in Berlin with his wife and two kids, and loves tennis and hiking (though, bizarrely, he constantly seems to find no time to do much of either those two). Thomas holds a Ph.D. in Computer Science from the Freie Universität Berlin.

Article navigation

Related articles

  • Test your skills: Understanding MongoDB Documents
  • How and when should I add timestamps to my MongoDB documents #Studio3T_AMA
  • How to Copy and Paste MongoDB Documents
  • Lesson 4: Understanding MongoDB Documents: Fields & Data Types
  • Querying Embedded Documents in MongoDB Arrays

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • Case Studies
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2022 3T Software Labs GmbH. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum
When you click "Accept", you are agreeing to cookies being on your device. They may improve site navigation, site usage analysis, or the relevance of messages. It is up to you which cookies are enabled. Read our Privacy Policy.
Manage cookies
Accept
✕

Privacy Preference Center

A cookie is a small file of letters and numbers that is downloaded on to your computer when you visit a website. Cookies are used by many websites and can do a number of things, eg remembering your preferences, recording what you have put in your shopping basket, and counting the number of people looking at a website. In the form below you can choose which cookies, except for essential cookies, to allow or disable.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected