Docs 菜单
Docs 主页
/
MongoDB 阿特拉斯
/ / /

如何迭代游标以查看所有结果

在此页面上

  • 创建 Atlas Search 索引
  • 运行示例查询

本教程介绍如何通过迭代游标直到耗尽来返回 Atlas Search 查询的所有结果。 默认情况下,某些 Atlas Search 客户端(例如 mongosh和 MongoDB Compass)最多会打印结果中的前20文档。 要一次性查看所有结果,我们建议您迭代游标直到耗尽。 为了演示如何遍历游标以查看所有结果,本教程将引导您完成以下步骤:

  1. sample_mflix.movies集合上设置具有动态映射的 Atlas Search 索引。

  2. 运行 Atlas Search 查询,迭代游标直到耗尽,以检索在title字段中包含术语summer的所有文档。

要创建 Atlas Search 索引,您必须拥有 Project Data Access Admin 或更高的项目访问权限。

在本节中,您将创建一个 Atlas Search 索引,该索引使用动态映射sample_mflix.movies 集合中的所有可动态索引字段动态创建索引。

1
  1. 如果尚未显示,请选择包含所需项目的组织导航栏中的Organizations菜单。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 如果 Clusters(数据库部署)页面尚未出现,请单击侧边栏中的 Database(数据库)。

    此时会显示“集群”页面。

2

您可以从侧边栏、 Data Explorer或集群详细信息页面转到 Atlas Search 页面。

  1. 在侧边栏中,单击Services标题下的Atlas Search

  2. Select data source下拉列表中,选择您的集群并单击Go to Atlas Search

    显示Atlas Search页面。

  1. 单击集群的对应 Browse Collections 按钮。

  2. 展开数据库并选择集合。

  3. 单击集合的Search Indexes标签页。

    显示Atlas Search页面。

  1. 单击集群名称。

  2. 单击 Atlas Search 标签页。

    显示Atlas Search页面。

3

单击 Create Search Index(连接)。

4
  • 要获得引导式体验,请选择 Atlas Search Visual Editor

  • 要编辑原始索引定义,请选择 Atlas SearchJSON Editor

5
  1. Index Name 字段中输入 iterate-cursor-tutorial

    注意

    如果将索引命名为 default,则在使用 $search 管道阶段时,您无需指定 index 参数。否则,您必须使用 index 参数指定索引名称。

  2. Database and Collection(数据库和集合)部分中找到 sample_mflix 数据库,然后选择 movies 集合。

6

以下索引定义对集合中的支持类型字段动态创建索引。您可以使用 Atlas Search Visual Editor 或 Atlas Search JSON Editor 在 Atlas 用户界面中创建索引。

  1. 单击 Next(连接)。

  2. 查看集合的默认索引定义。

  1. 单击 Next(连接)。

  2. 查看索引定义。

    索引定义应类似于以下内容:

    {
    "mappings": {
    "dynamic": true
    }
    }
  3. 单击 Next(连接)。

7
8

此时将显示一个模态窗口,让您知道索引正在构建中。单击 Close 按钮。

9

构建索引大约需要一分钟时间。在构建时,Status 列显示 Build in Progress。构建完成后,Status 列显示 Active


➤ 使用选择语言下拉菜单选择要用于运行此页面上的示例查询的 MongoDB 客户端。


在本部分中,您将连接到 Atlas 集群并对sample_mflix.movies集合中的title字段运行查询,以使用文本操作符搜索词语Summer 。该集合包括超过75个标题中包含Summer术语的文档,但默认情况下,某些 Atlas Search 客户端仅打印前20查询结果。

要一次性查看查询词的所有结果,可遍历游标,直到耗尽游标。

1

在终端窗口中打开mongosh并连接到集群。 有关连接的详细说明,请参阅通过mongosh连接。

2

mongosh 提示符下运行以下命令:

use sample_mflix
switched to db sample_mflix
3

在以下查询中,使用 toArray() 方法迭代游标,并返回数组中与查询条件匹配的文档。

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]

在以下查询中,使用forEach()方法迭代游标,并将 JavaScript 函数printjson应用于每个文档。

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}

注意

默认情况下,MongoDB Compass 显示前10个结果。

1

打开 MongoDB Compass 并连接到您的集群。有关连接的详细说明,请参阅通过 Compass 连接。

2

Databases 屏幕上,依次单击 sample_mflix 数据库和 movies 集合。

3

以下查询使用$search管道阶段来查询集合。

若要在 MongoDB Compass 中运行此查询:

  1. 单击 Aggregations 标签页。

  2. 单击 Select...,然后从下拉菜单中选择阶段并为该阶段添加查询,以配置以下每个管道阶段。单击 Add Stage 以添加其他阶段。

    管道阶段
    查询
    $search
    {
    "index": "iterate-cursor-tutorial",
    "text": {
    "query": "Summer",
    "path": "title"
    }
    }
    $project
    {
    "id": 0,
    "title": 1
    }
  3. 单击右上角的Export

  4. Export File Type 选择 JSON

  5. 单击 Export...(连接)。

  6. 打开 JSON文件以查看结果中的所有文档:

    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}]

后退

分页结果

来年

不区分变音符号