AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

결과 페이지 매김

MongoDB 7.0.5+를 실행 클러스터에서 MongoDB Search를 사용하여 점 이후 또는 이전에 $search 쿼리 결과를 순차적으로 조회 . $search searchAfter 또는 searchBefore 옵션을 사용하여 결과를 순서대로 탐색하고 애플리케이션에 '다음 페이지' 및 '이전 페이지' 함수를 빌드.

페이지가 매겨진 결과를 조회하려면 다음 단계를 수행하세요.

  1. 쿼리하려는 필드에 대한 인덱스를 만듭니다.

  2. Run a $search query that returns a point of reference. To learn more, see Retrieve Point of Reference.

  3. 후속 $search 쿼리에서 참조 지점을 사용하여 결과의 다음 또는 이전 문서 세트를 검색합니다.

    • To learn more about retrieving results to build a "Next Page" function, see Search After a Specific Point of Reference.

    • '이전 페이지' 함수를 빌드하기 위한 결과 검색에 대해 자세히 알아보려면 특정 참조 지점 전 Atlas 검색을 참조하세요.

    • To jump to a page in your results, combine $skip and $limit with $search searchAfter or searchBefore options. For example, to jump to results from Page 3 to Page 5, with 10 results per page, do the following:

      1. 3 페이지의 마지막 결과에 대한 기준점과 함께 searchAfter 을 사용하여 결과를 조회합니다(결과 30).

      2. Use $skip to skip the 10 results on Page 4 (Results 31-40) and $limit to limit the results to 10 documents.

      3. 5 페이지에 대한 결과를 반환합니다. (결과 41-50)

      Here, using $skip with the searchAfter option optimizes the query to skip only 1 page of results (10 documents). By comparison, if you use $skip without the $search searchAfter option, 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 bm25 similarity 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 different bm25 scores 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 to secondary or nearest.

      • 후속 쿼리에서 일관적인 점수를 보장하려면 필드를 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 $search query or in a separate $searchMeta query. This method is more efficient than using the $count stage after your $search stage.

    개수 옵션은 검색 기준과 일치하는 총 문서 수를 반환하므로 페이지가 매겨진 결과를 생성하는 경우 이후 쿼리에서 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.

searchSequenceToken 구문
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 검색 결과 정렬을 참조하세요.

참조 지점 이후를 검색하려면 $search 쿼리에서 searchAfter 옵션과 searchSequenceToken으로 생성된 토큰을 사용하여 참조 지점을 지정해야 합니다. searchSequenceToken에서 생성된 토큰은 searchSequenceToken 토큰을 생성한 $search 쿼리를 다시 실행할 때만 사용할 수 있습니다. 토큰을 사용하는 후속 $search 쿼리의 의미(검색 필드 및 값)는 searchSequenceToken이 토큰을 생성한 쿼리와 동일해야 합니다.

You can use the searchAfter option to build a "Next Page" function in your application. For a demonstration of this, see the example on this page.

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를 하려면 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}]

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:

  1. Load the sample_mflix.movies collection

  2. 동적 매핑을 사용하여 pagination-tutorial 인덱스 만듭니다. 인덱스 만드는 방법을 학습하려면 MongoDB Search 인덱스 관리를 참조하세요.

  3. Connect to your cluster using mongosh and switch to the sample_mflix database

참고

기본값 으로 MongoDB Search는 문서의 관련성 점수를 기준으로 결과의 문서를 정렬합니다. 결과의 여러 문서에 동일한 점수가 있는 경우 MongoDB Search는 임의로 정렬된 결과를 반환합니다. 문서를 정해진 순서로 반환하기 위해 쿼리는 고유한 필드released을 지정하여 결과를 정렬합니다.