MongoDB implementation paging (two methods)

Source: Internet
Author: User

1. Inserting experimental data

Lazy to use the samus,100 bar.

1                  for(inti =0; I < -; i++)2                 {3Document doc =NewDocument ();4doc["ID"] =i;5doc["MSG"] ="value is"+i;6 Mongocollection.insert (DOC);7}

Insert Successful

2. Paging

Method One: Through the Sike () and the Limit () method, for example,

by Db.myCollection.find (). Sort ({"ID": 1}). Skip (10) command, after sorting it by ID, skip 10, Query 10, and the result is 10-19 data.

This is similar to the operation of the Skip () and take () methods under the EF framework.

Method Two: Obtain the last record of the previous page, after the query of the specified bar record, such as,

Store the previous 10 records in TMP, latest the last record of the previous page (9th), in the next query, through the find parameters, filter to go to the previous record, get the following 10 records.

3. Comparison of two different methods

The method is relatively simple, but the skip method is inefficient, and it is more recommended to use method two in the case of large amounts of data, but it may take up more memory space.

MongoDB Implementation Paging (two methods)

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.