Navigation

Insert Documents

On this page

Important

Inserting documents is not permitted in MongoDB Compass Readonly Edition.

Compass provides two ways to insert documents into your collections: JSON Mode and a Field-by-Field Editor.

JSON Mode (New in Compass 1.20)
Allows you to write or paste JSON documents in the editor. Use this mode to insert multiple documents at once as an array.
Field-by-Field Editor
Provides a more interactive experience to create documents, allowing you to select individual field values and types. This mode only supports inserting one document at a time.

Procedure

To insert documents into your collection:

  1. Click the Add Data dropdown and select Insert Document.

    ../../_images/insert-button.png
  2. Select the appropriate view based on how you would like to insert documents.

    • Click the { } brackets for JSON view. This is the default view.
    • Click the list icon for Field-by-Field mode.

    ../../_images/insert-document-view-select.png

    1. 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 provide an ObjectId in your document, Compass automatically generates an ObjectId.

    2. Click Insert.


    1. For each field in the document, select field type and fill in the field name and value.

      ../../_images/insert-document2.png

      Note

      If you do not provide an ObjectId in your document, Compass automatically generates an ObjectId.

    Add New Fields

    To add a new field in the document, hover over the row number in the dialog (the row number is not part of the document but the dialog display) and click the plus icon 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.

    ../../_images/insert-document-new-field.png

    Change Field Type

    You can change the data type of a field by using the data type selectors on the right of the field.

    ../../_images/insert-document3.png

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

    ../../_images/insert-document4.png
    1. Click Insert.

Limitation

The Insert Document button is not available if you are connected to a Data Lake.