문서 메뉴
문서 홈
/
MongoDB 아틀라스
/ / /

커서를 반복하여 모든 결과를 보는 방법

이 페이지의 내용

  • Atlas Search 인덱스 만들기
  • 샘플 쿼리 실행

이 튜토리얼에서는 커서가 모두 사용될 때까지 커서를 반복하여 Atlas Search 쿼리에 대한 모든 결과를 반환하는 방법을 설명합니다. 기본적으로 mongosh 및 MongoDB Compass와 같은 일부 Atlas Search 클라이언트는 결과에서 처음 20 문서를 인쇄합니다. 모든 결과를 한 번에 보려면 커서가 모두 사용될 때까지 커서를 반복하는 것이 좋습니다. 이 튜토리얼에서는 커서를 반복하여 모든 결과를 보는 방법을 설명하기 위해 다음 단계를 안내합니다.

  1. sample_mflix.movies 컬렉션에 동적 매핑 을 사용하여 Atlas Search 인덱스를 설정합니다.

  2. title 필드에 summer 이라는 용어가 포함된 모든 문서를 검색하기 위해 커서가 소진될 때까지 커서를 반복하는 Atlas Search 쿼리를 실행합니다.

Atlas Search 인덱스를 생성하려면 프로젝트에 대한 Project Data Access Admin 이상의 액세스 권한이 있어야 합니다.

이 섹션에서는 동적 매핑을 사용하여 sample_mflix.movies 컬렉션에서 동적으로 인덱싱할 수 있는 모든 필드를 자동으로 인덱싱하는 Atlas Search 인덱스를 만듭니다.

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 를 클릭합니다.

  1. cluster의 Browse Collections 버튼을 클릭합니다.

  2. 데이터베이스를 확장하고 컬렉션을 선택합니다.

  3. 컬렉션의 Search Indexes 탭을 클릭합니다.

  1. 클러스터의 이름을 클릭합니다.

  2. Atlas Search 탭을 클릭합니다.

3

Create Search Index를 클릭합니다.

4
  • 안내를 따라 진행하고자 할 때는 Atlas Search Visual Editor를 선택합니다.

  • 원시 인덱스 정의를 편집하고 싶다면, Atlas Search JSON Editor을 선택합니다.

5
  1. Index Name 필드에 iterate-cursor-tutorial를 입력합니다.

    참고

    인덱스 이름을 default으로 지정하면 $search 파이프라인 단계를 사용할 때 index 매개 변수를 지정할 필요가 없습니다. 그렇지 않으면 index 매개 변수를 사용하여 인덱스 이름을 지정해야 합니다.

  2. Database and Collection 섹션에서 sample_mflix 데이터베이스를 찾고 movies 컬렉션을 선택합니다.

6

다음 인덱스 정의는 컬렉션에서 지원되는 형식의 필드를 동적으로 인덱싱합니다. Atlas user 인터페이스에서 Atlas Search Visual Editor 또는 Atlas Search JSON Editor을(를) 사용하여 인덱스를 생성할 수 있습니다.

  1. Next를 클릭합니다.

  2. collection의 기본 인덱스 정의를 검토합니다.

  1. Next를 클릭합니다.

  2. 인덱스 정의를 검토합니다.

    인덱스 정의는 다음과 비슷해야 합니다:

    {
    "mappings": {
    "dynamic": true
    }
    }
  3. Next를 클릭합니다.

7
8

인덱스가 작성 중임을 보여주는 모달 창이 표시됩니다. Close 버튼을 클릭합니다.

9

인덱스 작성에는 약 1분 정도가 소요됩니다. 작성하는 동안 Status 열은 Build in Progress을 읽습니다. 작성이 완료되면 Status 열에 Active이 표시됩니다.


언어 선택 드롭다운 메뉴를 사용하여 이 페이지의 예제 쿼리를 실행하는 데 사용할 MongoDB 클라이언트를 선택합니다.


이 섹션에서는 Atlas cluster에 연결하고 sample_mflix.movies 컬렉션의 title 필드에 대해 쿼리를 실행하여 텍스트 연산자로 Summer 라는 용어를 검색합니다. 컬렉션에는 제목에 Summer 라는 용어가 포함된 75 이상의 문서가 포함되어 있지만, 기본적으로 일부 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를 열고 cluster에 연결합니다. 연결에 대한 자세한 내용은 Compass를 통한 연결을 참조하세요.

2

Databases 화면에서 sample_mflix 데이터베이스를 클릭한 다음 movies collection을 클릭합니다.

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

돌아가기

페이지가 매겨진 결과

다음

분음 부호 무감지