Docs Menu

Docs HomeMongoDB Atlas

How to Iterate Your Cursor to View All Results

On this page

  • Required Access
  • Create the Atlas Search Index
  • Run the Sample Queries

This tutorial describes how to return all results for an Atlas Search query by iterating your cursor until it is exhausted. By default, some Atlas Search clients like mongosh and MongoDB Compass print up to the first 20 documents in the results. To view all results at once, we recommend that you iterate your cursor until it is exhausted. To demonstrate how to iterate your cursor to view all results, this tutorial takes you through the following steps:

  1. Set up an Atlas Search index with dynamic mapping on the sample_mflix.movies collection.

  2. Run an Atlas Search query that iterates your cursor until it is exhausted to retrieve all documents that contain the term summer in the title field.

To create an Atlas Search index, you must have Project Data Access Admin or higher access to the project.

In this section, you create an Atlas Search index that uses dynamic mapping to automatically index all the dynamically indexable fields in the sample_mflix.movies collection.

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. Click your cluster's name.

  4. Click the Atlas Search tab.

2

Click Create Search Index.

3
  • For a guided experience, select the Atlas Search Visual Editor.

  • To edit the raw index definition, select the Atlas Search JSON Editor.

4
  1. In the Index Name field, enter iterate-cursor-tutorial.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_mflix database, and select the movies collection.

5

The following index definition dynamically indexes the fields of supported types in the collection. You can use the Atlas Search Visual Editor or the Atlas Search JSON Editor in the Atlas user interface to create the index.

6
7

A modal window displays to let you know your index is building. Click the Close button.

8

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


Use the Select your language drop-down menu to select the MongoDB client that you want to use to run the example queries on this page.


In this section, you connect to your Atlas cluster and run queries against the title field in the sample_mflix.movies collection to search for the term Summer with the text operator. The collection includes over 75 documents that contain the term Summer in the title, but by default, some Atlas Search clients print only the top 20 results for the query.

To view all the results for the query term at once, you iterate through your cursor until you exhaust the cursor.

←  How to Divide Query Results into Discrete PagesHow to Define a Custom Analyzer and Run an Atlas Search Diacritic-Insensitive Query →
Share Feedback