MongoDB limit () sort () skip () count () method usage

Limit () method

If you need to read a specified number of data records in MongoDB, a method may be used MongoDB's Limit, limit () method takes a numeric parameter that specifies the number of records read from the MongoDB.
Syntax
limit () method of the basic syntax is as follows:

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

Examples of
data collection col 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 }
以上实例为显示查询文档中的两条记录:
> db.col.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
>

sort () method

MongoDB used in sort () method to sort the data, sort () method can be specified sort column by parameter, and using the 1 and -1 to specify the sort of way, in which 1 is ascending, and -1 for descending sort.
Syntax
sort () method substantially syntax is as follows:

>db.COLLECTION_NAME.find().sort({KEY:1})

Examples of
data collection col 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 }
以下实例演示了 col 集合中的数据按字段 likes 的降序排列:
>db.col.find({},{"title":1,_id:0}).sort({"likes":-1})
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
{ "title" : "MongoDB 教程" }
>

Skip () method

We read the specified amount of data may be used in addition to limit () method, but may also be used Skip () method to skip a specified number of data, Skip method also accepts as a parameter a number of the number of records to skip.
Syntax
skip () method script syntax is as follows:

>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
实例
以上实例只会显示第二条文档数据
>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
{ "title" : "Java 教程" }
>

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

MongoDB sort count () method

Use in MongoDB using the count () method for statistical data
similar in sql select count (*) from mycol

> db.mycol.count()
3

Guess you like

Origin blog.csdn.net/YPL_ZML/article/details/92800603