Two ways to MongoDB pagination (legend) _mongodb

Source: Internet
Author: User
Tags mongodb

Two ways to MongoDB pagination
MongoDB paging query is done by limit (), skip (), sort (), which is a combination of three functions for paging queries.
Here's my test data.
Db.test.find (). Sort ({"Age": 1});

The first of these methods
Query the first page of data: Db.test.find (). Sort ({"Age": 1}). Limit (2);



Query the data for the second page: Db.test.find (). Sort ({"Age": 1}). Skip (2). Limit (2);

Query other pages and so on ...

The second method
Query the first page of data: Db.test.find (). Sort ({"Age": 1}). Limit (2);

Same as the first method above.
Query the data for the second page:

This is to get the value of the last record on the first page and then exclude the previous record and get the new record.

In summary, if the amount of data is not very large, you can use the first method, after all, relatively simple, if the amount of data is relatively large, use the second method is better, because this can not skip () This function, skip skip too many records, the efficiency is a little low

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.