How to Iterate Your Cursor to View All Results
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:
Set up an Atlas Search index with dynamic mapping on the
sample_mflix.movies
collection.Run an Atlas Search query that iterates your cursor until it is exhausted to retrieve all documents that contain the term
summer
in thetitle
field.
Required Access
To create an Atlas Search index, you must have Project Data Access Admin
or higher access to the project.
Create the Atlas Search Index
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.
Navigate to the Atlas Search page for your project.
If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
Click your cluster's name.
Click the Atlas Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
iterate-cursor-tutorial
.Note
If you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.In the Database and Collection section, find the
sample_mflix
database, and select themovies
collection.
Specify an index definition.
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.
Run the Sample Queries
➤ 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.