对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

对结果进行分页

在运行MongoDB 7.0.5+ 的集群上,使用MongoDB Search 在参考点之后或之前按顺序检索$search查询结果。使用 $search searchAfter 或 searchBefore 选项按顺序遍历结果,并在应用程序中构建“下一页”和“上一个”函数。

要检索分页结果,请执行以下步骤:

  1. 在要查询的字段上创建索引。

  2. 运行将返回参考点的 $search 查询。要了解更多信息,请参阅检索参考点。

  3. 在随后的 $search 查询中使用参考点来检索结果中的下一组或上一组文档。

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

    • 要了解有关检索结果以构建“上一页”函数的更多信息,请参阅特定参考点之前的Atlas Search 。

    • 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. 使用searchAfter和第3页上最后一个结果的引用点(结果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 时出现重复和不一致。应用以下原则减少相关性分数:

    • 按唯一字段对查询进行排序,以防止相关性评分平局。有关示例,请参见按分数和唯一字段排序。

    • 如果要主要按非唯一字段进行排序,请在唯一字段上添加从节点(secondary node from replica set)排序子句以提供服务决胜局。

  • 在查询之间更新或删除文档可能会导致结果顺序不一致。应用以下原则以支持确定性搜索行为:

    • 通过不可变字段排序查询结果,比如 _id。MongoDB Search 反映您在初始查询和后续查询之间对集合进行的更新。如果按可变字段排序(例如 updated_time),并在第一个查询和第二个查询之间更新集合,对于相同文档,MongoDB Search 可能会不同。

    • 如果您部署了专用搜索节点,并按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 Search string 或 autocomplete 类型时,请将 similarity.type 属性设置为 stableTfl 或 boolean。这将强制 text、phrase、queryString 和 autocomplete 操作符使用 stableTfl 或 boolean 相似度算法来计算索引字段查询的相关性分数。这些算法在所有 MongoDB Search 节点中一致地计算分数。要了解更多信息,请参阅分数详情。

  • 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 查询分开继续计数,请使用带有 count 选项的 $searchMeta 阶段。

    对搜索结果进行计数可能会影响性能。仅在需要时使用计数选项,示例在检索第一页的搜索结果时。

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 生成词元的 $search 查询时,才可以使用由 searchSequenceToken 生成的词元。使用词元的后续 $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 ,您必须使用 searchBefore 选项和 searchSequenceToken 生成的词元在$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,对结果进行排序。