3.19.1. MongoDB
Limit()
Method ¶
If you need to read a specified number of data records in MongoDB, you can use the Limit method of MongoDB
limit()
Method accepts a numeric parameter that specifies the number of records read from the MongoDB.
3.19.2. Grammar ¶
limit()
The basic syntax of the method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER)
3.19.3. Example ¶
The data in the collection col is as follows:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 }
The following example shows two records in the query document:
> db.col.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
>
Note: if you do not specify
limit()
The parameters in the method display all the data in the collection.
3.19.4. MongoDB
Skip()
Method ¶
In addition to being able to use
limit()
Method to read a specified amount of data, you can also use the
skip()
Method to skip the specified amount of data, and the skip method also accepts a numeric parameter as the number of skipped records.
3.19.5. Grammar ¶
skip()
The syntax format of method script is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
3.19.6. Example ¶
The following example displays only the second document data
>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
{ "title" : "Java 教程" }
>
注:
skip()
方法默认参数为 0 。