Docs Menu
Docs Home
/
MongoDB Atlas
/ /

Run Atlas Search Queries

On this page

  • View Query Guidance Template
  • In Atlas, go to the Clusters page for your project.
  • Go to the Atlas Search page for your cluster.
  • View the query guidance template.
  • Run Atlas Search Queries in the Search Tester
  • Prerequisites
  • Search Your Collection
  • View, Edit, or Copy Query Syntax
  • Run Atlas Search Queries in Compass, Drivers and mongosh

You can run Atlas Search queries in the Search Tester, Compass, Drivers and mongosh. Atlas Search also provides query guidance in the Atlas UI.

Tip

Quickly try Atlas Search without needing an Atlas account, cluster, or collection, with the Atlas Search Playground. To learn more, see the documentation.

Atlas Search provides a sample compound query template with guidance. The template demonstrates how to build a compound query and the index definition for that query. For an example tutorial, see How to Run Atlas Search Compound Queries with Weighted Fields.

To view this query guidance template:

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If the Clusters page is not already displayed, click Database in the sidebar.

2

You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.

  1. In the sidebar, click Atlas Search under the Services heading.

  2. From the Select data source dropdown, select your cluster and click Go to Atlas Search.

  1. Click the Browse Collections button for your cluster.

  2. Expand the database and select the collection.

  3. Click the Search Indexes tab for the collection.

  1. Click the cluster's name.

  2. Click the Atlas Search tab.

3
  1. Click View Compound Query Example.

  2. Expand each operator to learn more.

  3. Click View Index Definition to view the sample definition.

The Search Tester feature in the Atlas Search tab allows you to:

  • Enter the term to search in the collection.

  • Run a $search or $searchMeta query. By default, the Search Tester shows a basic wildcard query.

  • View, edit, and copy your Atlas Search query syntax, which you can then run in mongosh or MongoDB Compass.

    Tip

    Atlas Search also provides templates for popular search queries that you can adapt.

For $search queries, the Search Tester returns the top 10 documents sorted based on relevance score. For $searchMeta queries, the Search Tester shows one meta document.

To use the Search Tester in the Atlas UI, you must have the following:

  • An Atlas cluster running MongoDB version 4.2 or higher.

  • Atlas Search index on your collection in the Atlas cluster.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If the Clusters page is not already displayed, click Database in the sidebar.

2

You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.

  1. In the sidebar, click Atlas Search under the Services heading.

  2. From the Select data source dropdown, select your cluster and click Go to Atlas Search.

  1. Click the Browse Collections button for your cluster.

  2. Expand the database and select the collection.

  3. Click the Search Indexes tab for the collection.

  1. Click the cluster's name.

  2. Click the Atlas Search tab.

3

On the index you'd like to query, click the Query button on the righthand side of the card.

4
  1. Enter the term to search in the search box.

  2. Click Search to search the collection.

1

Click Edit Query to view your query syntax in JSON format.

2

Atlas Search provides templates for popular search queries. To use a template:

  1. Click Create Query From Template. Atlas Search displays a list of popular search types.

  2. For a given search type, select a template from the Template drop-down menu.

  3. Click Insert for the template that you want to add.

3

You can edit or copy the query syntax in JSON format.

  1. Edit Query Syntax

    You can modify or replace the displayed query in the Query Editor and test your query by clicking the Search button.

    Important

    When you finish editing your query, be sure to copy it. Once you click Exit Query Editor, the Atlas UI discards your changes.

  2. Copy Query Syntax

    Click to copy the query syntax in JSON format to your clipboard. You can run the copied query in mongosh or MongoDB Compass after connecting to your Atlas cluster.

4

Note

The Atlas UI discards your changes when you exit the Query Editor.

To run a Atlas Search query, you must have the following:

  • An Atlas cluster running MongoDB version 4.2 or higher.

  • Atlas Search index on your collection in the Atlas cluster.

  • A connection to the client you want to use to run the query.

The Step 2: Run Atlas Search Queries page demonstrates how to connect to your Atlas cluster and run $search queries against the sample_mflix.movies collection using the following clients. To learn more, select a client using the Select your language drop-down menu on the Step 2: Run Atlas Search Queries page.

Client
Steps

MongoDB Compass

  1. Install and see Connect via Compass to connect to your Atlas cluster using MongoDB Compass.

  2. In the MongoDB Compass Aggregations tab, manually enter your aggregation pipeline.

To learn more, see Aggregation Pipeline Builder.

Drivers
  1. Install any one of the following drivers:

  2. See Connect via Drivers to connect to your Atlas cluster using the installed driver.

  3. Define and run an aggregation pipeline in your code editor.

To learn more about running $search queries using these drivers, see Step 2: Run Atlas Search Queries. Each example in the Step 2: Run Atlas Search Queries page performs the following:

  • Creates a connection to your Atlas cluster with the MongoClient.

  • Defines a pipeline.

  • Runs the pipeline.

  • Prints the query result.

MongoDB Shell
  1. Install and see Connect via mongosh to connect to your Atlas cluster using the MongoDB Shell.

  2. Define and run an Atlas Search query.

To learn more about running $search queries using mongosh, see Step 2: Run Atlas Search Queries.

Back

7. Return Stored Source Fields

Next

Explain Plan and Statistics