Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use Limit and Skip in MongoDB

Shulou Source: shulou.com Published: 2022-05-31 21:14:25 11月02日 Update

This article introduces how to use Limit and Skip in MongoDB. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Detailed explanation of the use of Limit and Skip in MongoDB

A MongoDB Limit () method

If you need to read a specified number of data records in MongoDB, you can use MongoDB's Limit method, and the limit () method accepts a numeric parameter that specifies the number of records to read from MongoDB.

Grammar

The basic syntax of the limit () method is as follows:

> db.COLLECTION_NAME.find () .limit (NUMBER)

Example

> db.col.find ({}, {"title": 1) .limit (2) {"title": "PHP tutorial"} {"title": "Java tutorial"} >

Note: if you do not specify the parameters in the limit () method, all the data in the collection is displayed.

Two MongoDB Skip () method

In addition to using the limit () method to read a specified amount of data, you can also use the skip () method to skip a specified amount of data, and the skip method also accepts a numeric parameter as the number of skipped records.

Grammar

The format of the skip () method script syntax is as follows:

> db.COLLECTION_NAME.find () .limit (NUMBER) .skip (NUMBER)

Example

The above example will only display the second document data

> db.col.find ({}, {"title": 1) .limit (1) .skip (1) {"title": "Java tutorial"} so much about how to use Limit and Skip in MongoDB. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Tags: Methods data parameters grammar examples tutorials quantities content numbers more help good usage interests guys partners documents articles formats knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Docker OPPO Reno Microsoft