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)

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)

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

Insert MongoDB documents to a collection

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

Download the latest version of Studio 3T here.

mongo shell syntax

To insert a document in mongo shell, input the insertOne() command. The syntax is as follows:

db.manatees.insert( { _id: "West Indian Manatee" } )

To insert multiple documents use insertMany() instead:

db.manatees.insert( { ["West Indian Manatee", "Amazonian Manatee"] } )

Studio 3T

Right-click on any cell while viewing a collection in Table, Tree, or JSON View.and choose Insert Document. Alternatively, use the hotkey 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.

Query MongoDB documents

mongo shell syntax

To query a document in MongoDB, input the find() command. This command will return every document that contains the specified keyword.

db.manatees.find( { _id: "West African Manatee" } )

Querying multiple documents with the AND condition is expressed as ‘,’ while the OR condition is expressed as $or. These can also be used together, as seen here:

db.manatees.find( { _id: "West African Manatee", "Amazonian Manatee" $OR: "West Indian Manatee" } )

Large databases often return many documents with each query. Organizing these documents is done with the pretty() cursor method. For more info check out MongoDB’s official documentation.

Studio 3T

Studio 3T offers four ways to query:

Visual Query Builder (drag-and-drop UI)

IntelliShell (built-in mongo shell with auto-completion)

SQL Query (SQL querying in MongoDB)

Aggregation Editor (stage-by-stage aggregation pipeline builder).

If you want to spare yourself the typing, the Visual Query Builder is a fast and easy way of running .find() commands just by dragging and dropping fields:

mongo shell syntax

Updating the values in a document is done through the update() command, as shown here:

db.manatees.update( { _id: "West Indian Manatee" }, { population: 6101 } )

Updating multiple documents at once requires the ‘multi’ parameter. The $set opratator allows the user to update specific fields within documents without changing anything else:

db.manatees.update( { likesMangroveLeaves: false }, { $set: { likesMangroveLeaves: true } }, { multi:true } )

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

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

Studio 3T

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.

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.

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

mongo shell syntax

You can delete documents through the mongo shell with the remove() command.

To remove all documents of a specific type, specify the _id .

db.manatees.remove( { _id: "elephant seals" } )

To ensure only one document of a type is deleted, use the justOne parameter:

db.manatees.remove( { _id: "elephant seals" }, { justOne: true } )

Lastly, to remove all documents in a collection, simply refrain from specifying any deletion criteria:

db.manatees.remove( { } )

Studio 3T

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:

Open the document in which the field to be renamed is located. Double click on the field name (In this case “dob”). You will now have the option to type the new field name into this space. 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:

Type in your query: {last:"Miller"} – and hit “Enter”. Open one of the documents resulting from the query search. Find the field that you would like to rename (“dob”) and right click it. Select “Rename Field”. Type in the new field name (“date_of_birth”) and hit “Enter”. Select “Documents matching query criteria” from the dropdown box. Click “Enter”. 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):