Docs Menu
Docs Home
/
MongoDB Atlas
/ / /

How to Iterate Your Cursor to View All Results

On this page

  • 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's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

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

  3. If it's not already displayed, click Clusters in the sidebar.

    The Clusters page displays.

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.

    The Atlas Search page displays.

  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.

    The Atlas Search page displays.

  1. Click the cluster's name.

  2. Click the Atlas Search tab.

    The Atlas Search page displays.

3

Click Create Search Index.

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

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

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

    If you name your index default, you don't need to specify an index parameter in the $search pipeline stage. If you give a custom name to your index, you must specify this name in the index parameter.

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

6

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.

  1. Click Next.

  2. Review the default index definition for the collection.

  1. Click Next.

  2. Review the index definition.

    Your index definition should look similar to the following:

    {
    "mappings": {
    "dynamic": true
    }
    }
  3. Click Next.

7
8

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

9

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.

1

Open mongosh in a terminal window and connect to your cluster. For detailed instructions on connecting, see Connect via mongosh.

2

Run the following command at mongosh prompt:

use sample_mflix
switched to db sample_mflix
3

In the following query, use the toArray() method to iterate the cursor and return the documents that match the query criteria in an array.

1db.movies.aggregate([
2 {
3 "$search": {
4 "index": "iterate-cursor-tutorial",
5 "text": {
6 "query": "summer",
7 "path": "title"
8 }
9 }
10 },
11 {
12 "$project": {
13 "_id": 0,
14 "title": 1
15 }
16 }
17]).toArray()
1[
2 { title: 'Summer' },
3 { title: 'Summer Stock' },
4 { title: 'Violent Summer' },
5 { title: 'Indian Summer' },
6 { title: 'Indian Summer' },
7 { title: 'Summer Rental' },
8 { title: 'Summer Things' },
9 { title: 'Wolf Summer' },
10 { title: 'Summer Storm' },
11 { title: 'Summer Palace' },
12 { title: 'Eternal Summer' },
13 { title: 'Summer Holiday' },
14 { title: 'Summer Wars' },
15 { title: 'Summer Games' },
16 { title: 'Summer Nights' },
17 { title: 'A Summer Place' },
18 { title: 'Summer and Smoke' },
19 { title: 'The Endless Summer' },
20 { title: "Summer of '42" },
21 { title: 'That Certain Summer' },
22 { title: 'One Deadly Summer' },
23 { title: 'Summer Camp Nightmare' },
24 { title: 'An Unforgettable Summer' },
25 { title: 'Summer of Sam' },
26 { title: 'Bullets Over Summer' },
27 { title: 'Summer in Berlin' },
28 { title: 'A Plumm Summer' },
29 { title: 'Summer Heights High' },
30 { title: 'Summer of Goliath' },
31 { title: 'Red Hook Summer' },
32 { title: 'Ping Pong Summer' },
33 { title: 'Summer of Blood' },
34 { title: 'The End of Summer' },
35 { title: 'Summer Wishes, Winter Dreams' },
36 { title: "A Summer at Grandpa's" },
37 { title: 'Cold Summer of 1953' },
38 { title: 'A Brighter Summer Day' },
39 { title: 'Summer of the Monkeys' },
40 { title: 'A Storm in Summer' },
41 { title: 'Wet Hot American Summer' },
42 { title: 'My Summer of Love' },
43 { title: 'Nasu: Summer in Andalusia' },
44 { title: 'A Summer in Genoa' },
45 { title: '(500) Days of Summer' },
46 { title: 'Summer Days with Coo' },
47 { title: 'The Kings of Summer' },
48 { title: 'May in the Summer' },
49 { title: 'A Horse for Summer' },
50 { title: 'The Summer of Sangaile' },
51 { title: 'Smiles of a Summer Night' },
52 { title: 'Shadows of a Hot Summer' },
53 { title: 'That Summer of White Roses' },
54 { title: 'Last Summer in the Hamptons' },
55 { title: 'A Summer in La Goulette' },
56 { title: 'A Summer by the River' },
57 { title: 'Summer in the Golden Valley' },
58 { title: 'How I Ended This Summer' },
59 { title: 'And They Call It Summer' },
60 { title: 'Spring, Summer, Fall, Winter... and Spring' },
61 { title: 'The Last Summer of La Boyita' },
62 { title: 'The Mafia Only Kills in Summer' },
63 { title: 'I Know What You Did Last Summer' },
64 { title: 'I Know What You Did Last Summer' },
65 { title: 'Judy Moody and the Not Bummer Summer' },
66 { title: 'I Still Know What You Did Last Summer' }
67]

In the following query, use the forEach() method to iterate the cursor and apply the JavaScript function printjson to each document.

1db.movies.aggregate([
2 {
3 "$search": {
4 "index": "iterate-cursor-tutorial",
5 "text": {
6 "query": "summer",
7 "path": "title"
8 }
9 }
10 },
11 {
12 "$project": {
13 "_id": 0,
14 "title": 1
15 }
16 }
17]).forEach(printjson)
1{
2 title: 'Summer'
3}
4{
5 title: 'Summer Stock'
6}
7{
8 title: 'Violent Summer'
9}
10{
11 title: 'Indian Summer'
12}
13{
14 title: 'Indian Summer'
15}
16{
17 title: 'Summer Rental'
18}
19{
20 title: 'Summer Things'
21}
22{
23 title: 'Wolf Summer'
24}
25{
26 title: 'Summer Storm'
27}
28{
29 title: 'Summer Palace'
30}
31{
32 title: 'Eternal Summer'
33}
34{
35 title: 'Summer Holiday'
36}
37{
38 title: 'Summer Wars'
39}
40{
41 title: 'Summer Games'
42}
43{
44 title: 'Summer Nights'
45}
46{
47 title: 'A Summer Place'
48}
49{
50 title: 'Summer and Smoke'
51}
52{
53 title: 'The Endless Summer'
54}
55{
56 title: "Summer of '42"
57}
58{
59 title: 'That Certain Summer'
60}
61{
62 title: 'One Deadly Summer'
63}
64{
65 title: 'Summer Camp Nightmare'
66}
67{
68 title: 'An Unforgettable Summer'
69}
70{
71 title: 'Summer of Sam'
72}
73{
74 title: 'Bullets Over Summer'
75}
76{
77 title: 'Summer in Berlin'
78}
79{
80 title: 'A Plumm Summer'
81}
82{
83 title: 'Summer Heights High'
84}
85{
86 title: 'Summer of Goliath'
87}
88{
89 title: 'Red Hook Summer'
90}
91{
92 title: 'Ping Pong Summer'
93}
94{
95 title: 'Summer of Blood'
96}
97{
98 title: 'The End of Summer'
99}
100{
101 title: 'Summer Wishes, Winter Dreams'
102}
103{
104 title: "A Summer at Grandpa's"
105}
106{
107 title: 'Cold Summer of 1953'
108}
109{
110 title: 'A Brighter Summer Day'
111}
112{
113 title: 'Summer of the Monkeys'
114}
115{
116 title: 'A Storm in Summer'
117}
118{
119 title: 'Wet Hot American Summer'
120}
121{
122 title: 'My Summer of Love'
123}
124{
125 title: 'Nasu: Summer in Andalusia'
126}
127{
128 title: 'A Summer in Genoa'
129}
130{
131 title: '(500) Days of Summer'
132}
133{
134 title: 'Summer Days with Coo'
135}
136{
137 title: 'The Kings of Summer'
138}
139{
140 title: 'May in the Summer'
141}
142{
143 title: 'A Horse for Summer'
144}
145{
146 title: 'The Summer of Sangaile'
147}
148{
149 title: 'Smiles of a Summer Night'
150}
151{
152 title: 'Shadows of a Hot Summer'
153}
154{
155 title: 'That Summer of White Roses'
156}
157{
158 title: 'Last Summer in the Hamptons'
159}
160{
161 title: 'A Summer in La Goulette'
162}
163{
164 title: 'A Summer by the River'
165}
166{
167 title: 'Summer in the Golden Valley'
168}
169{
170 title: 'How I Ended This Summer'
171}
172{
173 title: 'And They Call It Summer'
174}
175{
176 title: 'Spring, Summer, Fall, Winter... and Spring'
177}
178{
179 title: 'The Last Summer of La Boyita'
180}
181{
182 title: 'The Mafia Only Kills in Summer'
183}
184{
185 title: 'I Know What You Did Last Summer'
186}
187{
188 title: 'I Know What You Did Last Summer'
189}
190{
191 title: 'Judy Moody and the Not Bummer Summer'
192}
193{
194 title: 'I Still Know What You Did Last Summer'
195}

Note

By default, MongoDB Compass displays the first 10 results.

1

Open MongoDB Compass and connect to your cluster. For detailed instructions on connecting, see Connect via Compass.

2

On the Databases screen, click the sample_mflix database, then click the movies collection.

3

The following query uses the $search pipeline stage to query the collection.

To run this query in MongoDB Compass:

  1. Click the Aggregations tab.

  2. Click Select..., then configure each of the following pipeline stages by selecting the stage from the dropdown and adding the query for that stage. Click Add Stage to add additional stages.

    Pipeline Stage
    Query
    $search
    {
    "index": "iterate-cursor-tutorial",
    "text": {
    "query": "Summer",
    "path": "title"
    }
    }
    $project
    {
    "id": 0,
    "title": 1
    }
  3. At the top right corner, click Export.

  4. Select JSON for the Export File Type.

  5. Click Export....

  6. Open the JSON file to view all the documents in the results:

    1[{
    2 "title": "Summer"
    3 },
    4 {
    5 "title": "Summer Stock"
    6 },
    7 {
    8 "title": "Violent Summer"
    9 },
    10 {
    11 "title": "Indian Summer"
    12 },
    13 {
    14 "title": "Indian Summer"
    15 },
    16 {
    17 "title": "Summer Rental"
    18 },
    19 {
    20 "title": "Summer Things"
    21 },
    22 {
    23 "title": "Wolf Summer"
    24 },
    25 {
    26 "title": "Summer Storm"
    27 },
    28 {
    29 "title": "Summer Palace"
    30 },
    31 {
    32 "title": "Eternal Summer"
    33 },
    34 {
    35 "title": "Summer Holiday"
    36 },
    37 {
    38 "title": "Summer Wars"
    39 },
    40 {
    41 "title": "Summer Games"
    42 },
    43 {
    44 "title": "Summer Nights"
    45 },
    46 {
    47 "title": "A Summer Place"
    48 },
    49 {
    50 "title": "Summer and Smoke"
    51 },
    52 {
    53 "title": "The Endless Summer"
    54 },
    55 {
    56 "title": "Summer of '42"
    57 },
    58 {
    59 "title": "That Certain Summer"
    60 },
    61 {
    62 "title": "One Deadly Summer"
    63 },
    64 {
    65 "title": "Summer Camp Nightmare"
    66 },
    67 {
    68 "title": "An Unforgettable Summer"
    69 },
    70 {
    71 "title": "Summer of Sam"
    72 },
    73 {
    74 "title": "Bullets Over Summer"
    75 },
    76 {
    77 "title": "Summer in Berlin"
    78 },
    79 {
    80 "title": "A Plumm Summer"
    81 },
    82 {
    83 "title": "Summer Heights High"
    84 },
    85 {
    86 "title": "Summer of Goliath"
    87 },
    88 {
    89 "title": "Red Hook Summer"
    90 },
    91 {
    92 "title": "Ping Pong Summer"
    93 },
    94 {
    95 "title": "Summer of Blood"
    96 },
    97 {
    98 "title": "The End of Summer"
    99 },
    100 {
    101 "title": "Summer Wishes, Winter Dreams"
    102 },
    103 {
    104 "title": "A Summer at Grandpa's"
    105 },
    106 {
    107 "title": "Cold Summer of 1953"
    108 },
    109 {
    110 "title": "A Brighter Summer Day"
    111 },
    112 {
    113 "title": "Summer of the Monkeys"
    114 },
    115 {
    116 "title": "A Storm in Summer"
    117 },
    118 {
    119 "title": "Wet Hot American Summer"
    120 },
    121 {
    122 "title": "My Summer of Love"
    123 },
    124 {
    125 "title": "Nasu: Summer in Andalusia"
    126 },
    127 {
    128 "title": "A Summer in Genoa"
    129 },
    130 {
    131 "title": "(500) Days of Summer"
    132 },
    133 {
    134 "title": "Summer Days with Coo"
    135 },
    136 {
    137 "title": "The Kings of Summer"
    138 },
    139 {
    140 "title": "May in the Summer"
    141 },
    142 {
    143 "title": "A Horse for Summer"
    144 },
    145 {
    146 "title": "The Summer of Sangaile"
    147 },
    148 {
    149 "title": "Smiles of a Summer Night"
    150 },
    151 {
    152 "title": "Shadows of a Hot Summer"
    153 },
    154 {
    155 "title": "That Summer of White Roses"
    156 },
    157 {
    158 "title": "Last Summer in the Hamptons"
    159 },
    160 {
    161 "title": "A Summer in La Goulette"
    162 },
    163 {
    164 "title": "A Summer by the River"
    165 },
    166 {
    167 "title": "Summer in the Golden Valley"
    168 },
    169 {
    170 "title": "How I Ended This Summer"
    171 },
    172 {
    173 "title": "And They Call It Summer"
    174 },
    175 {
    176 "title": "Spring, Summer, Fall, Winter... and Spring"
    177 },
    178 {
    179 "title": "The Last Summer of La Boyita"
    180 },
    181 {
    182 "title": "The Mafia Only Kills in Summer"
    183 },
    184 {
    185 "title": "I Know What You Did Last Summer"
    186 },
    187 {
    188 "title": "I Know What You Did Last Summer"
    189 },
    190 {
    191 "title": "Judy Moody and the Not Bummer Summer"
    192 },
    193 {
    194 "title": "I Still Know What You Did Last Summer"
    195}]

Back

Paginated Results