Navigation

Documents

Note

Creating, deleting, editing and cloning documents is not permitted in MongoDB Compass Readonly Edition.

Documents Tab

From the Documents tab, you can view, insert, clone, modify, and delete documents.

Query Bar

Note

For query result sets larger than 1000 documents, Compass shows a sampling of the results. Otherwise, Compass shows the entire result set.

For details on sampling, see the FAQ.

From the query bar, you can specify the query, and if you click the Options, you can also specify query options.

../_images/query-bar-documents-view.png

You can type MongoDB queries into the query bar. In the query expression, you can use the various MongoDB query operators except for the $text query operator.

As you type, the Find button is disabled and the Filter label turns red until a valid query is entered. Filter syntax requires strict extended JSON types. For details on using the query bar, see Query Bar Options.

View Documents

Use the Documents tab to view the documents inside of a collection. Two different views are available to access documents: List View and Table View.

List View

When the Documents tab loads, Compass displays the List View, which shows all documents that match your query criteria (or all documents within the sample if no query criteria was specified). The documents are shown as individual members in a list format, as shown in the following example:

../_images/doc-view.png

To toggle the expansion of nested objects and array elements in a document, hover over the target document and click the arrow in the top-left corner.

Table View

Compass also provides the ability to view your documents in a table, rather than as individual items in a list.

Click the Table button to the right of the List button to access the document Table View.

../_images/doc-table-view.png

You can reorder the table columns by clicking and dragging the column headings. Reordering the columns is a strictly cosmetic change and does not change the underlying data itself.

To view nested object fields and array elements, hover your cursor over a field with a value type of Object or Array and click the button with outward-pointing arrows which appears on the right side of the field.

This opens a new tab in the Table View corresponding to the nested element.

../_images/table-view-expand-nested-elem.png

The following example displays the delays object nested within the flightStats collection documents. The new tab displays the two properties of the delay object: arrivalRunwayDelayMinutes and departureRunwayDelayMinutes.

../_images/doc-table-view-nested.png

To return to the original table display, click on the first tab in the Table View which displays the name of your collection.

Insert Documents

Click the Insert Document button to insert a new document:

../_images/insert-button.png

The insert dialog appears:

../_images/insert-document.png

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

../_images/insert-document2.png

Tip

For the field values, you can paste valid JSON that can be parsed by JavaScript’s JSON.parse.

  • If the pasted JSON is an Array, the first character must be a left bracket [ and the last character must be a right bracket ]:

    [ "first", "second", 3 ]
    
  • If the pasted JSON is an Object (i.e. a document), the keys must be quoted in double quotes and are permitted to be escaped. The first character must be left curly brace { and the last must be right curly brace }:

    {
      "email": "test@example.com",
      "phone": "555-555-1212"
    }
    

Add New Fields

To add a new field for 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 on the plus sign add a new field after the field.

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

You can also add a new field at the end by hitting the Tab from the value of the last field.

Tip

To add a new field after an array or a document field, hover over the row number in the dialog (the row number is not part of the document but the dialog display) and click on the plus sign add a new field after the field.

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

Clone Documents

It is also possible to insert new documents by cloning the schema and values of an existing document in a collection.

To clone a document, hover over the desired document and click the Clone button.

../_images/clone-doc.png

Cloning a document works the same way in the Table View as it does in the List View:

../_images/table-view-clone.png

Clicking the Clone button creates a new insert dialog with the same schema and values. You can edit any of these fields and values before you insert the new document.

../_images/clone-doc2.png

Modify Documents

To edit a document, hover over the document and select the pencil icon:

../_images/edit-doc.png

Modifying a document works the same way in the Table View as it does in the List View:

../_images/table-view-modify.png

After you click the pencil icon, the document enters edit mode:

../_images/edit-doc2.png

You can now make changes to the fields, values, or data types of values.

Tip

For the field values, you can paste valid JSON that can be parsed by JavaScript’s JSON.parse.

  • If the pasted JSON is an Array, the first character must be a left bracket [ and the last character must be a right bracket ]:

    [ "first", "second", 3 ]
    
  • If the pasted JSON is an Object (i.e. a document), the keys must be quoted in double quotes and are permitted to be escaped. The first character must be left curly brace { and the last must be right curly brace }:

    {
      "email": "test@example.com",
      "phone": "555-555-1212"
    }
    

Delete Fields

You can delete fields by selecting the x icon to the left of the field:

../_images/edit-doc3.png

Once selected, the field is marked for removal and appears highlighted in red:

../_images/edit-doc4.png

Add New Fields

To insert a new field, click or tab to the end of the document and fill in the field/value. New fields appear highlighted in green:

../_images/edit-doc5.png

To add a new field for 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 on the plus sign add a new field after the field.

You can also add a new field at the end by hitting the Tab from the value of the last field.

Tip

To add a new field after an array or a document field, hover over the row number in the dialog (the row number is not part of the document but the dialog display) and click on the plus sign add a new field after the field.

Modify Existing Field

You can modify documents by clicking on existing field names or values and making changes. In this example, the flight status was changed from L to M. Changed fields appear highlighted in yellow:

../_images/update-field.png

When you are finished editing the document, click the Update button to commit your changes. Compass performs a findAndModify operation to update the document.

Reverting a Change

While modifying a document, you have the option to revert changes made to a field prior to saving the modified document.

To revert changes to a document in the List View, hover over the edited field and click the revert icon which appears to the left of the field’s line number.

../_images/revert-doc-list-view.png

To revert changes to a document in the Table View, click the revert icon which appears on the right side of the edited table element.

../_images/revert-doc-table-view.png

Cancel Changes

To exit the edit mode and cancel all pending changes to the document, click the Cancel button.

Delete Documents

From the Documents view, you can delete documents by hovering over the document and selecting the trash icon:

../_images/delete-doc.png

Deleting a document works the same way in the Table View as it does in the List View:

../_images/table-view-delete.png

After clicking the delete button, the document is flagged for deletion and Compass asks for confirmation that you want to remove the document:

../_images/delete-doc2.png

After you confirm, Compass deletes the document from the collection.