MongoDB 7.0.5+를 실행 클러스터에서 MongoDB Search를 사용하여 점 이후 또는 이전에 $search 쿼리 결과를 순차적으로 조회 . $search searchAfter 또는 searchBefore 옵션을 사용하여 결과를 순서대로 탐색하고 애플리케이션에 '다음 페이지' 및 '이전 페이지' 함수를 빌드.
사용법
페이지가 매겨진 결과를 조회하려면 다음 단계를 수행하세요.
쿼리하려는 필드에 대한 인덱스를 만듭니다.
후속
$search쿼리에서 참조 지점을 사용하여 결과의 다음 또는 이전 문서 세트를 검색합니다.'다음 페이지' 함수를 빌드하기 위한 결과 검색에 대해 자세히 알아보려면 특정 기준점 이후 Atlas 검색을 참조하세요.
'이전 페이지' 함수를 빌드하기 위한 결과 검색에 대해 자세히 알아보려면 특정 참조 지점 전 Atlas 검색을 참조하세요.
To jump to a page in your results, combine
$skipand$limitwith$searchsearchAfterorsearchBeforeoptions. For example, to jump to results from Page 3 to Page 5, with 10 results per page, do the following:3 페이지의 마지막 결과에 대한 기준점과 함께
searchAfter을 사용하여 결과를 조회합니다(결과 30).Use
$skipto skip the 10 results on Page 4 (Results 31-40) and$limitto limit the results to 10 documents.5 페이지에 대한 결과를 반환합니다. (결과 41-50)
Here, using
$skipwith thesearchAfteroption optimizes the query to skip only 1 page of results (10 documents). By comparison, if you use$skipwithout the$searchsearchAfteroption, the query skips 4 pages of results (40 documents). To learn more, see Jump from Page 2 to Page 5 Using searchAfter and $skip.
고려 사항
여러 문서가 동일한 값을 가진 필드를 기준으로 정렬할 때 타이가 발생합니다. MongoDB는 연결된 쿼리 결과의 순서를 보장하지 않으므로
searchAfter및searchBefore를 사용할 때 중복 및 불일치가 발생할 수 있습니다. 다음 원칙을 적용하여 연관성 점수 동점을 줄이세요.관련성 점수의 동점을 방지하기 위해 쿼리를 고유 필드로 정렬합니다. 예시는 점수 및 고유 필드별 정렬을 참조하세요.
고유하지 않은 필드 기준으로 주로 정렬하려면 고유 필드 에 세컨더리 정렬 절을 추가하여 순위 결정자 제공 .
쿼리 간에 문서를 업데이트하거나 삭제하면 결과 순서에 일관성 문제가 발생할 수 있습니다. 결정론적 검색 동작을 지원하도록 다음 원칙을 적용합니다.
쿼리 결과를
_id과 같은 불변 필드로 정렬합니다. MongoDB 검색은 초기 쿼리와 후속 쿼리 사이에 컬렉션에 가한 업데이트를 반영합니다.updated_time과 같은 변경 가능한 필드로 정렬한 후 첫 번째와 두 번째 쿼리 사이에 컬렉션을 업데이트하면, MongoDB 검색이 동일한 문서를 다르게 정렬할 수 있습니다.전용 검색 노드를 배포하고
searchScore로 결과를 정렬하는 경우 다음을 고려합니다.By default, MongoDB Search scores documents using the
bm25similarity algorithm, which calculates term frequency in relation to the entire corpus of documents on the MongoDB Search node. Because each MongoDB Search node replicates from the changestream independently, this corpus might vary across MongoDB Search nodes. As a result, the same query can return differentbm25scores when routed to different MongoDB Search nodes. Subsequent queries are more likely to be routed to different MongoDB Search nodes if your deployment uses dedicated MongoDB Search nodes, or your read preference is set tosecondaryornearest.후속 쿼리에서 일관적인 점수를 보장하려면 필드를 MongoDB 검색 string 또 autocomplete 유형으로 인덱스할 때
similarity.type속성을stableTfl또는boolean으로 설정합니다. 이렇게 하면text, phrase, queryString, autocomplete 연산자가stableTfl또는boolean유사성 알고리즘을 사용하여 색인된 필드에 대한 쿼리의 관련성 점수를 계산하도록 강제합니다. 이 알고리즘은 모든 MongoDB 검색 노드에서 일관되게 점수를 계산합니다. 자세한 내용은 점수 상세 정보를 참조하세요.
To count search results during pagination, use the count option in your initial
$searchquery or in a separate$searchMetaquery. This method is more efficient than using the$countstage after your$searchstage.개수 옵션은 검색 기준과 일치하는 총 문서 수를 반환하므로 페이지가 매겨진 결과를 생성하는 경우 이후 쿼리에서
count옵션을 사용할 필요가 없습니다. 메인$search쿼리와 별도로 계산하려면$searchMeta단계를count옵션과 함께 사용합니다.검색 결과를 집계하면 성능에 영향을 미칠 수 있습니다. 개수 옵션은 첫 번째 페이지에 대한 검색 결과를 조회할 때와 같이 필요한 경우에만 사용합니다.
기준점 조회
To retrieve query results at a certain point, you must provide the point of reference in your $search query. You can retrieve the reference point by using the $meta keyword searchSequenceToken in the $project stage after your $search stage.
구문
1 [{ 2 "$search": { 3 "index": "<index-name>", 4 "<operator-name>"|"<collector-name>": { 5 <operator-specification>|<collector-specification> 6 }, 7 "sort": { 8 "score": { 9 "$meta": "searchScore" 10 } 11 }, 12 ... 13 }, 14 { 15 "$project": { 16 "paginationToken" : { "$meta" : "searchSequenceToken" } 17 }, 18 ... 19 }]
출력
searchSequenceToken 은 결과의 각 문서에 대해 기본64인코딩된 토큰을 생성합니다. 토큰의 길이는 쿼리의 정렬 옵션에 지정된 필드 수에 따라 늘어납니다. 토큰은 데이터베이스의 스냅샷에 연결되지 않습니다.
쿼리 에 sort 옵션을 지정하지 않는 한 결과의 문서는 기본값 순서로 정렬됩니다. 결과 정렬에 대해 학습하려면 MongoDB 검색 결과 정렬을 참조하세요.
특정 참조 지점 후 Atlas Search
참조 지점 이후를 검색하려면 $search 쿼리에서 searchAfter 옵션과 searchSequenceToken으로 생성된 토큰을 사용하여 참조 지점을 지정해야 합니다. searchSequenceToken에서 생성된 토큰은 searchSequenceToken 토큰을 생성한 $search 쿼리를 다시 실행할 때만 사용할 수 있습니다. 토큰을 사용하는 후속 $search 쿼리의 의미(검색 필드 및 값)는 searchSequenceToken이 토큰을 생성한 쿼리와 동일해야 합니다.
searchAfter 옵션을 사용하여 애플리케이션에서 '다음 페이지' 함수를 빌드할 수 있습니다. 이에 대한 데모는 이 페이지의 예제 를 참조하세요.
searchAfter 구문
1 [{ 2 "$search": { 3 "index": "<index-name>", 4 "<operator-name>"|"<collector-name>": { 5 <operator-specification>|<collector-specification> 6 }, 7 "searchAfter": "<base64-encoded-token>", 8 "sort": { 9 "score": { 10 "$meta": "searchScore" 11 } 12 }, 13 ... 14 }, 15 "$project": { 16 "paginationToken" : { "$meta" : "searchSequenceToken" } 17 }, 18 ... 19 }]
출력
MongoDB Search returns the documents in the results after the specified token. MongoDB Search returns the generated tokens for the documents in the results because you specified the searchSequenceToken in the $project stage after the $search stage (as shown in line 11). These tokens can be used as a reference point for another query with the same semantics.
쿼리 에 sort 옵션을 지정하지 않는 한 결과의 문서는 기본값 순서로 정렬됩니다. 결과 정렬에 대해 학습하려면 MongoDB 검색 결과 정렬을 참조하세요.
특정 참조 지점 이전 Atlas Search
기준점 앞에서 Atlas Search를 하려면 searchSequenceToken 에 의해 생성된 토큰과 함께 searchBefore 옵션을 사용하여 $search 쿼리에서 기준점을 지정해야 합니다. searchSequenceToken 이(가) 토큰을 생성한 $search 쿼리를 다시 실행하는 경우에만 searchSequenceToken 에서 생성된 토큰을 사용할 수 있습니다. 토큰을 사용하는 후속 $search 쿼리의 시맨틱(Atlas Search 필드 및 값)은 searchSequenceToken 토큰을 생성한 쿼리와 동일해야 합니다.
searchBefore 옵션을 사용하여 애플리케이션 에서 ' 이전 페이지' 함수를 빌드 할 수 있습니다. 이렇게 하려면 다음을 결합합니다.
이에 대한 데모는 이 페이지의 searchBefore 쿼리 예제 를 참조하세요.
searchBefore 구문
1 [{ 2 "$search": { 3 "index": "<index-name>", 4 "<operator-name>"|"<collector-name>": { 5 <operator-specification>|<collector-specification> 6 }, 7 "searchBefore": "<base64-encoded-token>", 8 "sort": { 9 "score": { 10 "$meta": "searchScore" 11 } 12 }, 13 ... 14 }, 15 "$project": { 16 "paginationToken" : { "$meta" : "searchSequenceToken" } 17 }, 18 ... 19 }]
searchBefore 출력
MongoDB Search returns documents in the results preceding the specified token in reverse order. MongoDB Search also returns the generated tokens for the documents in the results because you specified the searchSequenceToken in the $project stage after the $search stage (as shown in line 11). These tokens can be used as a reference point for another query with the same semantics.
예시
The following examples use the db.collection.aggregate() method to run $search queries against the sample_mflix database. The first example query demonstrates how to retrieve a point of reference, which you use in the subsequent example queries to retrieve additional results for pages after or before the reference point.
To run these example queries in mongosh:
Load the sample_mflix.movies collection
동적 매핑을 사용하여
pagination-tutorial인덱스 만듭니다. 인덱스 만드는 방법을 학습하려면 MongoDB Search 인덱스 관리를 참조하세요.Connect to your cluster using
mongoshand switch to thesample_mflixdatabase
참고
기본값 으로 MongoDB Search는 문서의 관련성 점수를 기준으로 결과의 문서를 정렬합니다. 결과의 여러 문서에 동일한 점수가 있는 경우 MongoDB Search는 임의로 정렬된 결과를 반환합니다. 문서를 정해진 순서로 반환하기 위해 쿼리는 고유한 필드released을 지정하여 결과를 정렬합니다.
샘플 쿼리는 다음 파이프라인 단계를 사용하여 첫 번째 페이지에 대한 결과를 검색하고 토큰 또는 후속 쿼리에 대한 참조 지점을 검색합니다.
파이프라인 단계 | 설명 |
|---|---|
결과를 | |
결과에는 문서의
|
db.movies.aggregate([ { "$search": { "index": "pagination-tutorial", "text": { "path": "title", "query": "summer" }, "sort": { "released": 1 } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "released": 1, "genres": 1, "paginationToken": { "$meta" : "searchSequenceToken" }, "score": { "$meta": "searchScore" } } } ])
[ { genres: [ 'Drama' ], title: "A Summer at Grandpa's", paginationToken: 'CKUdGgJgAA==', score: 2.262615203857422 }, { genres: [ 'Musical', 'Romance' ], title: 'Summer Stock', released: ISODate('1951-01-22T00:00:00.000Z'), paginationToken: 'CJsFGgkpAHw/0HT///8=', score: 3.000213623046875 }, { genres: [ 'Comedy', 'Romance' ], title: 'Smiles of a Summer Night', released: ISODate('1957-12-23T00:00:00.000Z'), paginationToken: 'CKIHGgkpAKDlpaf///8=', score: 2.0149309635162354 }, { genres: [ 'Drama' ], title: 'Violent Summer', released: ISODate('1959-11-13T00:00:00.000Z'), paginationToken: 'CI8JGgkpAJhJh7X///8=', score: 3.000213623046875 }, { genres: [ 'Drama', 'Romance' ], title: 'A Summer Place', released: ISODate('1959-11-18T00:00:00.000Z'), paginationToken: 'CLoJGgkpAGQJobX///8=', score: 2.579726457595825 }, { genres: [ 'Drama' ], title: 'The End of Summer', released: ISODate('1962-02-01T00:00:00.000Z'), paginationToken: 'CK0KGgkpAAzP18X///8=', score: 2.262615203857422 }, { genres: [ 'Drama', 'Romance' ], title: 'Summer and Smoke', released: ISODate('1962-04-01T00:00:00.000Z'), paginationToken: 'CMQKGgkpAECmB8f///8=', score: 2.579726457595825 }, { genres: [ 'Documentary', 'Sport' ], title: 'The Endless Summer', released: ISODate('1968-08-17T00:00:00.000Z'), paginationToken: 'CO4MGgkpAJjH5vX///8=', score: 2.579726457595825 }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: "Summer of '42", released: ISODate('1971-04-09T00:00:00.000Z'), paginationToken: 'CPQQGgkpAGRgUAkAAAA=', score: 2.579726457595825 }, { genres: [ 'Drama' ], title: 'That Certain Summer', released: ISODate('1972-11-01T00:00:00.000Z'), paginationToken: 'COwRGgkpAPQV0hQAAAA=', score: 2.579726457595825 } ]
참고
검색 결과 카운트
페이지 매김 중에 검색 결과를 계산하려면 $searchMeta 쿼리를 카운트 옵션을 사용하여 별도로 실행. 검색 기준과 일치하는 총 문서 수가 포함된 메타데이터 문서 반환합니다.
다음 샘플 쿼리 title 필드 에 summer 이라는 텀 포함된 총 문서 수를 가져옵니다.
db.movies.aggregate([ { "$searchMeta": { "index": "pagination-tutorial", "text": { "path": "title", "query": "summer" }, "count": { "type": "lowerBound", "threshold": 5000 } } } ])
[ { count: { lowerBound: Long("65") } } ]
추가 결과를 조회하려면 결과를 조회할 참조 지점을 지정합니다.
샘플 쿼리는 다음 파이프라인 단계를 사용하여 동일한 용어에 대한 이전 쿼리에서 searchSequenceToken에 의해 생성된 토큰을 사용하여 두 번째 페이지의 결과를 조회합니다.
파이프라인 단계 | 설명 |
|---|---|
| |
결과를 | |
결과에는 문서의
|
db.movies.aggregate([ { "$search": { "index": "pagination-tutorial", "text": { "path": "title", "query": "summer" }, "searchAfter": "COwRGgkpAPQV0hQAAAA=", "sort": { "released": 1 } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "released": 1, "genres": 1, "paginationToken" : { "$meta" : "searchSequenceToken" }, "score": { "$meta": "searchScore" } } } ])
[ { genres: [ 'Drama' ], title: 'Summer Wishes, Winter Dreams', released: ISODate('1974-09-09T00:00:00.000Z'), paginationToken: 'CMwSGgkpAECHcCIAAAA=', score: 2.262615203857422 }, { genres: [ 'Drama', 'Thriller' ], title: 'Shadows of a Hot Summer', released: ISODate('1978-09-01T00:00:00.000Z'), paginationToken: 'CPEVGgkpAGw/qz8AAAA=', score: 2.0149309635162354 }, { genres: [ 'Drama' ], title: 'Indian Summer', released: ISODate('1978-11-01T00:00:00.000Z'), paginationToken: 'CNYRGgkpAFhj5UAAAAA=', score: 3.000213623046875 }, { genres: [ 'Drama' ], title: 'Indian Summer', released: ISODate('1978-11-01T00:00:00.000Z'), paginationToken: 'CNsRGgkpAFhj5UAAAAA=', score: 3.000213623046875 }, { genres: [ 'Drama', 'Mystery' ], title: 'One Deadly Summer', released: ISODate('1983-05-11T00:00:00.000Z'), paginationToken: 'COwcGgkpAAjtIGIAAAA=', score: 2.579726457595825 }, { genres: [ 'Comedy' ], title: 'Summer Rental', released: ISODate('1985-08-09T00:00:00.000Z'), paginationToken: 'CL8fGgkpABTypHIAAAA=', score: 3.000213623046875 }, { genres: [ 'Drama', 'Romance' ], title: 'Summer', released: ISODate('1986-08-29T00:00:00.000Z'), paginationToken: 'CO0gGgkpAHCiY3oAAAA=', score: 3.5844719409942627 }, { genres: [ 'Drama', 'Thriller' ], title: 'Summer Camp Nightmare', released: ISODate('1987-04-17T00:00:00.000Z'), paginationToken: 'CNkiGgkpAHQ/CX8AAAA=', score: 2.579726457595825 }, { genres: [ 'Action', 'Crime', 'Drama' ], title: 'Cold Summer of 1953', released: ISODate('1988-06-01T00:00:00.000Z'), paginationToken: 'CNsjGgkpACjVTYcAAAA=', score: 2.262615203857422 }, { genres: [ 'Drama', 'War' ], title: 'That Summer of White Roses', released: ISODate('1989-07-11T00:00:00.000Z'), paginationToken: 'CI0mGgkpALSEc48AAAA=', score: 2.0149309635162354 } ]
이전 결과를 조회하려면 결과를 조회하기 전에 조회할 참조 지점을 지정합니다.
샘플 쿼리는 다음 파이프라인 단계를 사용하여 동일한 용어에 대한 이전 쿼리에서 searchSequenceToken으로 생성된 토큰을 사용하여 두 번째 페이지의 결과를 검색합니다.
파이프라인 단계 | 설명 |
|---|---|
| |
결과를 | |
결과에는 문서의
|
참고
By default, MongoDB Search returns the results in reverse order for queries that specify tokens to retrieve results before a point of reference. To return documents in-order, the query uses the toArray() and the JavaScript reverse() methods.
db.movies.aggregate([ { "$search": { "index": "pagination-tutorial", "text": { "path": "title", "query": "summer" }, "searchBefore": "CMwSGgkpAECHcCIAAAA=", "sort": { "released": 1 } } }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "released": 1, "genres": 1, "paginationToken" : { "$meta" : "searchSequenceToken" }, "score": { "$meta": "searchScore" } } } ]).toArray().reverse()
[ { genres: [ 'Drama' ], title: "A Summer at Grandpa's", paginationToken: 'CKUdGgJgAA==', score: 2.262615203857422 }, { genres: [ 'Musical', 'Romance' ], title: 'Summer Stock', released: ISODate('1951-01-22T00:00:00.000Z'), paginationToken: 'CJsFGgkpAHw/0HT///8=', score: 3.000213623046875 }, { genres: [ 'Comedy', 'Romance' ], title: 'Smiles of a Summer Night', released: ISODate('1957-12-23T00:00:00.000Z'), paginationToken: 'CKIHGgkpAKDlpaf///8=', score: 2.0149309635162354 }, { genres: [ 'Drama' ], title: 'Violent Summer', released: ISODate('1959-11-13T00:00:00.000Z'), paginationToken: 'CI8JGgkpAJhJh7X///8=', score: 3.000213623046875 }, { genres: [ 'Drama', 'Romance' ], title: 'A Summer Place', released: ISODate('1959-11-18T00:00:00.000Z'), paginationToken: 'CLoJGgkpAGQJobX///8=', score: 2.579726457595825 }, { genres: [ 'Drama' ], title: 'The End of Summer', released: ISODate('1962-02-01T00:00:00.000Z'), paginationToken: 'CK0KGgkpAAzP18X///8=', score: 2.262615203857422 }, { genres: [ 'Drama', 'Romance' ], title: 'Summer and Smoke', released: ISODate('1962-04-01T00:00:00.000Z'), paginationToken: 'CMQKGgkpAECmB8f///8=', score: 2.579726457595825 }, { genres: [ 'Documentary', 'Sport' ], title: 'The Endless Summer', released: ISODate('1968-08-17T00:00:00.000Z'), paginationToken: 'CO4MGgkpAJjH5vX///8=', score: 2.579726457595825 }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: "Summer of '42", released: ISODate('1971-04-09T00:00:00.000Z'), paginationToken: 'CPQQGgkpAGRgUAkAAAA=', score: 2.579726457595825 }, { genres: [ 'Drama' ], title: 'That Certain Summer', released: ISODate('1972-11-01T00:00:00.000Z'), paginationToken: 'COwRGgkpAPQV0hQAAAA=', score: 2.579726457595825 } ]
결과를 건너뛰고 페이지 2에서 5로 이동하려면 searchSequenceToken에서 생성된 토큰을 사용하여 결과를 조회할 참조 지점을 지정한 후 결과에서 문서 20개를 건너뛰세요.
The sample query uses the following pipeline stages to jump to results on page 5 by using the token generated by searchSequenceToken from the prior query for the same term and by using the $skip and $limit stages:
파이프라인 단계 | 설명 |
|---|---|
| |
Skips 20 documents in the results after the specified point of reference, which is the token associated with the twentieth document in the results for the query you ran to Retrieve Page 2 Using searchAfter. | |
결과를 | |
결과에는 문서의
|
db.movies.aggregate([ { "$search": { "index": "pagination-tutorial", "text": { "path": "title", "query": "summer" }, "searchAfter": "COwRGgkpAPQV0hQAAAA=", "sort": { "released": 1 } } }, { "$skip": 20 }, { "$limit": 10 }, { "$project": { "_id": 0, "title": 1, "released": 1, "genres": 1, "paginationToken" : { "$meta" : "searchSequenceToken" }, "score": { "$meta": "searchScore" } } } ])
[ { genres: [ 'Drama' ], title: 'A Storm in Summer', released: ISODate('2000-02-27T00:00:00.000Z'), paginationToken: 'CO5FGgkpAChakN0AAAA=', score: 2.262615203857422 }, { genres: [ 'Comedy', 'Romance' ], title: 'Wet Hot American Summer', released: ISODate('2002-04-11T00:00:00.000Z'), paginationToken: 'CKtIGgkpAFBUIu0AAAA=', score: 2.262615203857422 }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'Summer Things', released: ISODate('2002-10-09T00:00:00.000Z'), paginationToken: 'CIpPGgkpAFxzxvAAAAA=', score: 3.000213623046875 }, { genres: [ 'Adventure', 'Drama', 'Family' ], title: 'Wolf Summer', released: ISODate('2003-02-28T00:00:00.000Z'), paginationToken: 'COZWGgkpAGS6ofMAAAA=', score: 3.000213623046875 }, { genres: [ 'Animation', 'Adventure' ], title: 'Nasu: Summer in Andalusia', released: ISODate('2003-06-26T00:00:00.000Z'), paginationToken: 'CNlaGgkpAMxoAfYAAAA=', score: 2.262615203857422 }, { genres: [ 'Drama' ], title: 'Spring, Summer, Fall, Winter... and Spring', released: ISODate('2004-05-28T00:00:00.000Z'), paginationToken: 'CJ5ZGgkpAOjnyPwAAAA=', score: 1.8161234855651855 }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'Summer Storm', released: ISODate('2004-09-02T00:00:00.000Z'), paginationToken: 'CMVfGgkpAMRwvP4AAAA=', score: 3.000213623046875 }, { genres: [ 'Drama' ], title: 'Summer in the Golden Valley', released: ISODate('2004-10-08T00:00:00.000Z'), paginationToken: 'CNNWGgkpALTVdf8AAAA=', score: 2.0149309635162354 }, { genres: [ 'Drama', 'Romance' ], title: 'My Summer of Love', released: ISODate('2005-07-01T00:00:00.000Z'), paginationToken: 'CL5aGgkpAEyxzwQBAAA=', score: 2.262615203857422 }, { genres: [ 'Drama' ], title: 'Summer in Berlin', released: ISODate('2006-01-05T00:00:00.000Z'), paginationToken: 'CPZmGgkpANzclwgBAAA=', score: 2.579726457595825 } ]
MongoDB Search facet (MongoDB Search 연산자)를 사용하여 결과를 그룹 하려면 string 필드 token 유형으로 인덱스 해야 합니다. 다음 쿼리 실행 하고 movies 컬렉션 의 genres 필드 기준으로 결과를 그룹 하려면 인덱스 다음 예시 와 유사해야 합니다.
{ "mappings": { "dynamic": true, "fields": { "genres": { "type": "token" } } } } }
샘플 쿼리는 다음 파이프라인 단계를 사용합니다:
파이프라인 단계 | 설명 |
|---|---|
| |
Adds the | |
결과를 | |
다음과 같은 필드를 반환합니다.
|
db.movies.aggregate([ { "$search": { "index": "pagination-tutorial", "facet": { "operator": { "text": { "path": "title", "query": "summer" } }, "facets": { "genresFacet": { "type": "string", "path": "genres" } } } } }, { "$addFields": { "paginationToken" : { "$meta" : "searchSequenceToken" } } }, { "$limit": 10 }, { "$facet": { "docs": [ { "$project": { "_id": 0, "title": 1, "released": 1, "genres": 1, "paginationToken" : 1 } } ], "meta": [ { "$replaceWith": "$$SEARCH_META" }, { "$limit": 1 } ] } }, { "$set": { "meta": { "$arrayElemAt": ["$meta", 0] } } } ])
[ { docs: [ { genres: [ 'Drama', 'Romance' ], title: 'Summer', released: ISODate('1986-08-29T00:00:00.000Z'), paginationToken: 'CO0gFf1nZUA=' }, { genres: [ 'Musical', 'Romance' ], title: 'Summer Stock', released: ISODate('1951-01-22T00:00:00.000Z'), paginationToken: 'CJsFFYADQEA=' }, { genres: [ 'Drama' ], title: 'Violent Summer', released: ISODate('1959-11-13T00:00:00.000Z'), paginationToken: 'CI8JFYADQEA=' }, { genres: [ 'Drama' ], title: 'Indian Summer', released: ISODate('1978-11-01T00:00:00.000Z'), paginationToken: 'CNYRFYADQEA=' }, { genres: [ 'Drama' ], title: 'Indian Summer', released: ISODate('1978-11-01T00:00:00.000Z'), paginationToken: 'CNsRFYADQEA=' }, { genres: [ 'Comedy' ], title: 'Summer Rental', released: ISODate('1985-08-09T00:00:00.000Z'), paginationToken: 'CL8fFYADQEA=' }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'Summer Things', released: ISODate('2002-10-09T00:00:00.000Z'), paginationToken: 'CIpPFYADQEA=' }, { genres: [ 'Adventure', 'Drama', 'Family' ], title: 'Wolf Summer', released: ISODate('2003-02-28T00:00:00.000Z'), paginationToken: 'COZWFYADQEA=' }, { genres: [ 'Comedy', 'Drama', 'Romance' ], title: 'Summer Storm', released: ISODate('2004-09-02T00:00:00.000Z'), paginationToken: 'CMVfFYADQEA=' }, { genres: [ 'Drama', 'Romance' ], title: 'Summer Palace', released: ISODate('2007-04-18T00:00:00.000Z'), paginationToken: 'CIRrFYADQEA=' } ], meta: { count: { lowerBound: Long('65') }, facet: { genresFacet: { buckets: [ { _id: 'Drama', count: Long('48') }, { _id: 'Romance', count: Long('20') }, { _id: 'Comedy', count: Long('19') }, { _id: 'Family', count: Long('7') }, { _id: 'Adventure', count: Long('5') }, { _id: 'Crime', count: Long('5') }, { _id: 'Mystery', count: Long('5') }, { _id: 'Thriller', count: Long('5') }, { _id: 'Horror', count: Long('4') }, { _id: 'Action', count: Long('3') } ] } } } } ]