For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Insert Documents

Important

Inserting documents is not permitted in MongoDB Compass Readonly Edition.

Compass provides the following view modes to insert documents into your collections:

Shell Syntax Mode
Write or paste documents in the MongoDB shell syntax, which allows unquoted field names and type constructors such as ObjectId() and ISODate(). Use this mode to insert multiple documents at once as an array.
Field-by-Field Editor
Select individual field values and types for each inserted document. This mode only supports inserting one document at a time.
JSON Mode
Write or paste documents in Extended JSON format, which requires quoted field names and represents BSON types as wrappers such as {"$oid": "..."}. Use this mode to insert multiple documents at once as an array.

To insert documents into your collection, select the tab corresponding to your chosen view mode:

1

In the drop-down menu, click Insert Document.

Insert button
click to enlarge

Alternatively, you can right-click the query bar or its surrounding whitespace and select Insert document... from the drop-down menu.

2

In the Insert Document modal, click the icon to use Shell Syntax mode to insert documents into your collection.

Shell Syntax mode is the default view.

3

In the MongoDB shell syntax, type or paste the document(s) you want to insert into the collection. To insert multiple documents, enter a comma-separated array of documents.

Example

The following example inserts a single document into the collection. Field names are unquoted, and the _id, copies, and lastCheckedOut values use the ObjectId(), NumberInt(), and ISODate() type constructors instead of Extended JSON type wrappers:

{
_id: ObjectId('507f1f77bcf86cd799439011'),
title: 'The Odyssey',
author: 'Homer',
copies: NumberInt('10'),
lastCheckedOut: ISODate('2025-03-14T09:30:00Z')
}

Note

If you do not specify an _id value in your document, Compass automatically generates an ObjectId for the _id field.

4
1

In the drop-down menu, click Insert Document.

Insert button
click to enlarge

Alternatively, you can right-click the query bar or its surrounding whitespace and select Insert document... from the drop-down menu.

2

In the Insert Document modal, click the icon for Field-by-Field Editor mode.

3

To add a new field in the document, hover over the row number in the dialog and click the icon to add a new field after the selected row.

You can also add a new field at the end of the document by pressing the tab key when your text cursor is in the value of the last document field.

Note

If you do not specify an _id value in your document, Compass automatically generates an ObjectId for the _id field.

4

You can change the data type of a field by clicking the the data type drop-down selector on the right of the field.

To change the _id field to use a custom value, change the data type from ObjectID to string and then overwrite the _id value.

5
1

In the drop-down menu, click Insert Document.

Insert button
click to enlarge

Alternatively, you can right-click the query bar or its surrounding whitespace and select Insert document... from the drop-down menu.

2

In the Insert Document modal, click the icon for JSON mode.

3

In JSON format, type or paste the document(s) you want to insert into the collection. To insert multiple documents, enter a comma-separated array of JSON documents.

Example

The following array inserts 5 documents into the collection:

[
{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },
{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },
{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 },
{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 },
{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 }
]

Note

If you do not specify an _id value in your document, Compass automatically generates an ObjectId for the _id field.

4
Rate this page

On this page