MongoDB Limit and Skip Methods
MongoDB Limit () method
If you need to read a specified number of data records in MongoDB, you can use the Limit Method of MongoDB and the limit () method to accept a number parameter, which specifies the number of records read from MongoDB.
Syntax
The basic syntax of the limit () method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER)
Instance
The data in the Set 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 ":" cainiao tutorial "," url ":" http://www.runoob.com "," tags ": [" php "]," likes ": 200} {"_ id": ObjectId ("56066549ade2f21f36b0313b"), "title": "Java tutorial", "description ": "Java is a high-level programming language launched by Sun Microsystems in May 1995. "," By ":" cainiao tutorial "," url ":" http://www.runoob.com "," tags ": [" java "]," likes ": 150} {"_ id": ObjectId ("5606654fade2f21f36b0313c"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database", "": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb"], "likes": 100}
The above example shows 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 set is displayed.
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 the specified amount of data, the skip method also accepts a number parameter as the number of records skipped.
Syntax
The script syntax format of the skip () method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Instance
The above instance only displays 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.
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: