Docs Menu
Docs Home
/
MongoDB Atlas
/ / /

How to Divide Query Results into Discrete Pages

On this page

  • Create the Atlas Search Index With Dynamic Mapping
  • Run a Query and Paginate the Results

Note

Looking for Sequential Pagination to build infinite scrolling, "Next Page", and "Previous Page" functions?

Atlas Search queries might return many results. To make navigating results easier, you can use the following aggregation pipeline stages to paginate your query results:

  1. $skip bypasses the specified number of documents that pass into the stage and passes the remaining documents to the next stage in the pipeline.

  2. $limit restricts the number of documents passed to the next stage in the pipeline.

You can extend this method in your applications to jump across pages and show results non-sequentially.

Note

Optimize performance with $search searchSequenceToken

We recommend using $skip after $search with the searchAfter or searchBefore options. To learn more, see Jump to a Page Non-Sequentially.

You can also use the SEARCH_META Aggregation Variable to return the total number of search results. You can use this information in your applications to show the total number of search results, or the number of pages available to your application user.

This tutorial takes you through the following steps:

  1. Set up an Atlas Search index with dynamic mapping for the sample_mflix.movies collection.

  2. Run an Atlas Search query using the following:

Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.

To create an Atlas Search index, you must have Project Data Access Admin or higher access to the project.

In this section, we create an Atlas Search index that uses dynamic mapping to index the fields in the sample_mflix.movies collection.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. Click your cluster's name.

  4. Click the Atlas Search tab.

2
3
  • For a guided experience, select the Atlas Search Visual Editor.

  • To edit the raw index definition, select the Atlas Search JSON Editor.

4
  1. In the Index Name field, enter pagination-tutorial.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_mflix database, and select the movies collection.

5

You can create an Atlas Search index that uses dynamic mappings through the Atlas Search Visual Editor or Atlas Search JSON Editor in the Atlas User Interface. The following index definition dynamically indexes the fields of supported types in the movies collection.

  1. Click Next.

  2. Review the default index definition for the movies collection.

  1. Click Next.

  2. Review the index definition.

    Your index definition should look similar to the following:

    {
    "mappings": {
    "dynamic": true
    }
    }

    The above index definition dynamically indexes the fields of supported types in each document in the movies collection.

  3. Click Next.

6
7

A modal window appears to let you know your index is building. Click the Close button.

8

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


Use the Select your language drop-down menu to set the language of the example in this section.


In this section, we run Atlas Search queries using the following:

You can run one or all of the following queries against the collection using the same index definition.

← How to Paginate Query Results