Navigation

Query Bar

From the Documents, Schema, and Explain Plan tabs, you can use the query bar to specify MongoDB queries to return a subset of documents from the collection.

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

You can specify a query filter in the query bar. If you click Options to expand the query bar, you also have access to the project, sort, collation, skip, and limit options of the query.

Filter The query.
Project The projection to determine the fields to return. Specify the field and 1 to include a field or 0 to exclude a field.
Sort The sort order of the returned documents. To specify ascending order for a field, specify the field and 1. To specify descending order for a field, specify the field and -1.
Collation The collation to use for the query. Use this option to specify language-specific rules for string comparison.
Skip The number of matching documents to skip before returning documents.
Limit The upper limit for the number of documents to return.

Note

MongoDB Compass does not support the $near and $nearSphere geospatial query operators in any of the query bar fields, because they do not provide any additional functionality in the schema view.

Query Bar Options

Filter

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

Tip

In the Schema tab, you can also use the Query Builder to enter a query into the query bar.

As you type, the Find button is disabled and the Filter label turns red until a valid query is entered. The Compass Filter supports using the mongo shell mode representation of the MongoDB Extended JSON BSON data types.

Example

The following filter returns documents where start_date is greater than than the BSON Date 2017-05-01:

{ "start_date": {$gt: new Date('2017-05-01')} }

By specifying the Date type in both start_date and the $gt comparison operator, Compass performs the greater than comparison chronologically, returning documents with start_date later than 2017-05-01.

Without the Date type specification, Compass compares the start_dates as strings lexicographically, instead of comparing the values chronologically.

Filter by ObjectID

To specify an ObjectID in your filter, use the format ObjectId('<id>'), as in the following filter example:

{ "_id": ObjectId('59a87101f17fcbfbc9cd4374') }

Query Bar Autocompletion

The query bar autocompletes the current query based on the keys in your collection’s documents, including keys in embedded subdocuments.

Project

If the query bar has the Project option, you can specify which fields to return in the resulting data. By default, all fields are returned.

To set projection, click Options and enter in the projection document:

  • To include fields, specify the field name and set to 1 in the project document, as in the following example:

    { year: 1, name: 1 }
    

    Only the fields specified in the project document (and the _id field if it is not set to 0 in project document) are returned.

  • To exclude fields, specify the field name and set to 0 in the project document, as in the following example:

    { year: 0, name: 0 }
    

    All fields except for the fields specified in the project document are returned.

Note

With the exception of the _id field, you cannot specify both field inclusion and field exclusion in the project document. With the _id field, you can specify its exclusion in a project document that specifies field inclusions. For example, { year: 1, _id: 0 }.

For more information on projection, refer to the MongoDB Manual.

Sort

If the query bar has the Sort option, you can specify the sort order of the returned documents.

To set the sort order, click Options and enter in the sort document:

To specify ascending order for a field, set the field to 1 in the sort document. To specify descending order for a field, set the field and -1 in the sort documents. For example, the following sort document sorts results first by year in descending order, and within each year, sort by name in ascending order.

{ year: -1, name: 1 }

For more information on sort, refer to the MongoDB Manual.

Collation

Use the Collation query bar option to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.

Use the locale field in the collation document to specify the ICU Locale code for the desired language. For a complete list of supported languages and locales, refer to the MongoDB Manual.

Example

To use the pinyin variant of the Chinese collation, use the following collation document:

{ "locale" : "zh@collation=pinyin" }

For a detailed list of all possible fields in a collation document, refer to the MongoDB Manual.

Limit

If the query bar has the Limit option, you can specify the maximum number of documents to return. To specify the limit, click Options and enter the limit.

For more information on limit, refer to the MongoDB Manual.

Run the Query

Click Find to run the query and view the updated results.

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.

To clear the query bar and the results of the query, click Reset.

Export the Query

New in version 1.15.0

MongoDB Compass supports exporting queries in a variety of languages for use in applications utilizing the MongoDB driver available for that language. For details, see Export Query to Specific Language.

Past Queries View

Compass automatically stores up to 20 most recent queries for each collection. To view these queries, click the ellipsis (…) button in the query bar, then click Toggle Query History.

../_images/query-history-view.png

From the Past Queries view, you can view recent queries and queries saved as favorites for the current collection.

Recent Queries

You can view up to 20 of the most recent queries for that collection. Click a listed query to populate the query bar with that query.

../_images/query-history-select.png

You can also hover over a query and perform the following actions:

  • Click the Star icon to save the query as a favorite. Specify a name for the query and click Save.
  • Click the Copy icon to copy the query to the clipboard.
  • Click the Trash icon to remove the query from the Recent list.

Favorite Queries

From the list of recent queries for a collection, you can save queries as favorites. To view the list of queries saved as favorites, click on Favorites in the past queries view.

../_images/query-history-favorite.png