MongoDB method of Quick Page _mongodb

Source: Internet
Author: User
Tags mongodb

Paging through data is one of the most common operations of MongoDB. A typical scenario is the need to display your results in your user interface. If you are dealing with data in batches, it is also important to make your paging strategy correct so that your data processing can be scaled.

Next, let's take a look at the different ways that you can flip through data in MongoDB. In this example, we have a CRM database of user data that we need to browse through and display at the same time 10 users. So in fact, our page size is 10. Below is the structure of our user documentation:

{

  _id,

  name, company

  ,

  State

}

method One: Using Skip () and limit ()

The MONGODB itself supports paging operations using the Skip () and limit () directives. The Skip (n) instruction tells MongoDB that it should skip the "n" Result and limit (n) instruction indicating MongoDB, which should limit the result to "n". Normally, you will use the Skip () and limit () instructions, but in order to illustrate the situation, we provide the console command so that we can achieve the same result. At the same time, for the simplicity of the code, the restriction check code is excluded.

Page 1

db.users.find (). Limit ()

//page 2

db.users.find (). Skip (limit)//page

3

Db.users.find (). Skip (limit) ......



In general, to retrieve the page n, the code is like this:

Db.users.find (). Skip (pagesize* (n-1)). Limit (pagesize)
However, this approach has serious performance problems as the size of the data increases. The reason is that the complete result set is established each time the query is executed, and the server must go from the collected start to the specified offset. This process becomes more and more slow as the offset increases. At the same time, the process does not use indexes effectively. So, when you have smaller datasets, the typical "skip ()" and "Limit ()" methods are useful. If you are using a large dataset, you need to consider other methods.

Method Two: Using Find () and limit ()

The previous method cannot be extended well because of the skip () command. Therefore, the goal of this section is to implement paging without using the Skip () command. To do this, we will take advantage of the natural order in which data is stored, such as timestamps or identities stored in documents.

In this example, we will use "_id" to store each document. "_id" is a MONGODB objectid structure, that is, the 12-byte structure contains timestamps, machining, process identifiers, counters, and so on. The general idea is as follows:

Retrieves the last document in the current page _id
Retrieving files on the next page is greater than this "_id"

Page 1

db.users.find (). limit (pageSize);

Find the ID of the "last document in" page

last_id = ...

Page 2

users = Db.users.find ({' _id ' > last_id}). limit (a);

Update the last ID with the ID of the "last document in" page

last_id = ...

This method uses intrinsic rules for the existence of _id fields. Also because the "_id" field is the default lookup operation, it is a very good performance metric. If the field you are using is not indexed, you will be bothered with the operation, so it is important to make sure that the fields are indexed.

Also, if you want the data to be sorted in a specific order, you can also use the sort () clause with the above technique. It is important to ensure that the sorting process takes advantage of indexes for optimal performance. You can use the. explain () suffix to query.

Users = Db.users.find ({' _id ' > last_id}). Sort (..). Limit (ten);

Update the last ID with the ID of the "last document in" page

last_id = ...

The above is the way of MongoDB fast page, hope to give you a reference, but also hope that we support cloud habitat community.

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.