Home Database Mysql Tutorial How does mongoDB implement paging?

How does mongoDB implement paging?

Jul 03, 2017 pm 04:39 PM
mongodb Pagination how

This article mainly introduces in detail the two methods of mongoDB to implement paging, which has certain reference value. Interested friends can refer to it

Paging of mongoDBQuery is performed by combining the three functions of limit(), skip(), sort() array for paging query.

The following is my test data

db.test.find().sort({"age":1});

##The first method

Query the data on the first page: db.test.find().sort({"age":1}).limit(2);

Query the data on the second page: db.test.find().sort({"age":1}).skip(2).limit(2) ;

Query the number of other pages and so on. . .

The second method

Query the data on the first page: db.test.find().sort({"age":1}).limit( 2);

# is the same as the first method above.

Query the data on the second page:

This is to get the value of the last record on the first page, and then exclude the previous records to get it New record

In summary, if the amount of data is not very large, you can use the first method. After all, it is relatively simple. If the amount of data is relatively large, it is better to use the second method, because this You don’t need to use the skip() function

, skip skips too many records, and the efficiency is a bit lowAfter careful consideration, the second method is indeed not suitable for page skipping, and the efficiency It’s not very high either

For massive data, we need to do some special processing.

There are the following two methods

The first method

Limit the number of paging pages, similar to Baidu's paging processing, which only displays the previous seven hundred records, like this There is no need to consider performance issues. After all, most people just turn to the first ten pages and find what they need.

The statistical results below should be estimated, based on the proportion of these records found. Estimate the total number of records

Second methodWe can do this, assuming it is sorted according to id, we can id follow The serial number of the page where the id is located is stored in redis/MemberCached,

Just like this, assuming that each page has 10 records

id page

1 1

twenty one

. . .

10 1

11 2

12 2

. . . .

20 2

In this way, when we check the first page, we can directly retrieve ten pieces of data.

Assume there are 100 million pieces of data, and a record id occupies 4 bytes. Other information occupies one byte, and one record occupies 5 bytes

1 0000 0000 *5/(1024*1024)=476MB

This approach uses space for time, which is generally Most of the database query time is spent on the connection to the database. Putting it in the cache can greatly speed up the query speed

The above is the detailed content of How does mongoDB implement paging?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to identify genuine and fake shoe boxes of Nike shoes (master one trick to easily identify them) How to identify genuine and fake shoe boxes of Nike shoes (master one trick to easily identify them) Sep 02, 2024 pm 04:11 PM

As a world-renowned sports brand, Nike's shoes have attracted much attention. However, there are also a large number of counterfeit products on the market, including fake Nike shoe boxes. Distinguishing genuine shoe boxes from fake ones is crucial to protecting the rights and interests of consumers. This article will provide you with some simple and effective methods to help you distinguish between real and fake shoe boxes. 1: Outer packaging title By observing the outer packaging of Nike shoe boxes, you can find many subtle differences. Genuine Nike shoe boxes usually have high-quality paper materials that are smooth to the touch and have no obvious pungent smell. The fonts and logos on authentic shoe boxes are usually clear and detailed, and there are no blurs or color inconsistencies. 2: LOGO hot stamping title. The LOGO on Nike shoe boxes is usually hot stamping. The hot stamping part on the genuine shoe box will show

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

How to deal with video jitter (practical tips to help you eliminate video jitter) How to deal with video jitter (practical tips to help you eliminate video jitter) Sep 02, 2024 pm 03:53 PM

Shaking is a common problem when shooting or watching videos, which affects the viewing experience and reduces the quality of the video. This article will introduce some practical tips to help you deal with video jitter problems and make your videos more stable and smooth. 1. Use Stabilizer Technology to Eliminate Video Shake Using a stabilizer device is one of the simplest and most effective ways to solve the problem of video shake. Stabilizers can reduce jitter caused by hand shaking or other factors by balancing and stabilizing the camera. 2. Introduction to software video stabilization technology Software video stabilization technology eliminates jitter by adjusting the video in post-processing. This technology can provide better video stabilization by tracking key frames, applying image stabilization algorithms, and more. 3. Video jitter detection and automatic repair

How to make your bank deposits more cost-effective (money saving strategy revealed) How to make your bank deposits more cost-effective (money saving strategy revealed) Aug 21, 2024 pm 04:21 PM

In modern society, we are all inseparable from bank accounts, and saving money is the most basic interaction between us and banks. However, many people have certain doubts and confusions about how to make their savings more cost-effective. This article will provide you with some practical money saving advice to help you increase the value of your savings. Paragraph 1 Financial Plan: A blueprint for future wealth growth. Developing a financial plan is the basis for effectively managing and growing your savings. Identify your financial goals, both short- and long-term. Develop a specific savings plan based on these goals, setting the time, amount, and method of deposit required for each goal. Regularly review and adjust your plan to adapt to changing economic conditions and personal needs. Paragraph 2 Choosing a High-Interest Savings Account: Increase Return on Deposits Choose a High-Interest Rate

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

See all articles