Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use MongoDB for quick paging

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Using MongoDB Quick Pagination

Original English text:

http://blog.mongodirector.com/fast-paging-with-mongodb/

Pagination through your data is one of the most common operations with MongoDB. A typical case is to display results in a table in your UI. If you are batch processing data, it is important that the paging strategy is correct so that your data processing is scalable.

Let's take a look at different ways to paginate data in MongoDB with an example. In this example, we have a user database CRM and we need to paginate and display 10 users at once. So our pagination size is 10. This is our user documentation structure:

{_id,name,company,state}

Method 1: Use skip() and limit()

MongoDB natively supports paging operations using the skip() and limit() commands. The skip(n) command tells MongoDB to skip n results, and the limit(n) command tells MongoDB to limit results to "n" results. Typically you will use skip() and limit() commands via cursors-but to illustrate this case we provide console commands to accomplish the same result. Because of the simplicity of the code, it also eliminates the limitation of examining the code.

//Page 1db.users.find().limit (10)//Page 2db.users.find().skip(10).limit(10)//Page 3db.users.find().skip(20).limit(10)........

You got it. Usually get page n code like this:

db.users.find().skip(pagesize*(n-1)).limit(pagesize)

However, as the data size grows, this approach has serious performance problems. The reason is that each time the query executes, the complete result set is constructed, and then the instance must be located at a specific offset from the beginning of the set. As your drift increases, the process becomes slower and slower. This process also does not make efficient use of indexes. Thus typically "skip()" and "limit()" methods are effective for small data sets. If you are using large data sets, you need to consider other approaches.

Method 2: Use find() and limit()

The reason the previous method didn't extend very well is because of the skip() command. So the goal of this section is to perform paging without using the "skip()" command. We store data in natural order using timestamps or ids in documents. In this example, we use the "_id" stored in each document. "_id" is a MongoDB ObjectID structure, a 12-byte structure containing a timestamp, machine, process ID, counter, and so on. The overall idea is as follows:

1. Get_id of last document on current page

2. Get documents on the next page greater than this "_id"

//Page 1db.users.find().limit(pageSize);//Find the id of the last document in this pagelast_id = ...// Page 2users = db.users.find({'_id'> last_id}). limit(10);//Update the last id with the id of the last document in this pagelast_id = ...

This method preserves the inherent order that exists in the "_id" column. Also because the "_id" column is indexed by default, lookup performance is very good. If the column you're using isn't indexed, your performance will suffer--so it's important to make sure that column is indexed.

If you also want to arrange data in a particular order for your pagination, you can use the sort() clause with the above technique. It is important to ensure that the sorting process overrides the index for optimal performance. You can use the.explain suffix to your query to decide.

users = db.users.find({'_id'> last_id}). sort(..). limit(10);//Update the last id with the id of the last document in this pagelast_id = ...

As always, if you have any questions or comments, please feel free to contact us at support@mongodirector.com

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report