MongoDB Limit and Skip methods

Source: Internet
Author: User
Tags numeric mongodb mongodb limit mongodb tutorial php tutorial
MongoDB Limit () method

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

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

>db. Collection_name.find (). Limit (number)
Example

The data in the collection col is as follows:

{"_id": ObjectId ("56066542ade2f21f36b0313a"), "title": "PHP Tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites. "By": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["PHP"], "likes": $
{"_id": ObjectId ("56066549ade 2f21f36b0313b ")," title ":" Java Tutorial "," description ":" Java is a high-level programming language introduced by Sun Microsystems Company in May 1995. "By": "Rookie Tutorial", "url": "Http://www.runoob.com", "tags": ["Java"], "likes": [] {"
_id": ObjectId ("5606654fad e2f21f36b0313c ")," title ":" MongoDB Tutorial "," description ":" MongoDB is a Nosql database "," by ":" Rookie Tutorial "," url ":" Http://www.runo Ob.com "," tags ": [" MongoDB "]," likes ": 100}

The above example shows the two records in the query document:

> db.col.find ({},{"title": 1,_id:0}). Limit (2)
{"title": "PHP Tutorial"}
{"title": "Java Tutorial"}
>

Note: If you do not specify a parameter in the limit () method, all data in the collection is displayed. MongoDB Skip () method

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

The Skip () method script syntax is formatted 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,_id:0}). Limit (1). Skip (1)
{"title": "Java Tutorial"}
>

Note: The default parameter of the Skip () method is 0.

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.