mongodb-Foundation-limit-skip-sort

Source: Internet
Author: User
Tags mongodb limit 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)
> 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)
Instance

Show the second article:

>db.col.find ({},{"title": 1,_id:0}). Limit (1). Skip (1) {"title": "Java Tutorial"}> Note:The default parameter for the Skip () method is 0. Skip is the meaning of skipping, the previous example of limit (1) indicates that the first record starts with a number of 1, which shows the second. However, after skipping, it means to skip 1, that is, to perform the skip jump before the limit. MongoDB sort () method

Using the sort () method to sort data in MongoDB, the sort () method can specify a sorted field by parameter, and use 1 and-one to specify how the sort is sorted, where 1 is in ascending order, and 1 is used in descending order.

Grammar

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

>db. Collection_name.find (). Sort ({key:1})
The data in the instance Col collection is as follows:> Db.col.find () {"_id": ObjectId ("577f62810fe1ae602eda934e"), "title": "Java", "likes": 100}{"_id": ObjectId ("577f62960fe1ae602eda934f"), "title": "php", "Likes": *} {"_id": ObjectId ("577f62ae0fe1ae602eda9350"), "tit Le ":" C + +, "likes": "_id": ObjectId ("577f62c60fe1ae602eda9351"), "title": ". Net", "Likes": 93} Likes in ascending order:> Db.col.find ({},{' title ': 1, ' Likes ': 1,_id:0}). Sort ({"Likes": 1}) {"title": "php", "likes": "{"}} "title": "C + +", "likes" : the "title": ". Net", "likes": "{"} {"title": "Java", "likes": 100}:> Db.col.find in descending order of likes ({},{' title ': 1, ' like S ': 1,_id:0}). Sort ({"Likes": -1}) {"title": "Java", "likes": ". Net", "likes": "{"}} "title": "C + +", "Li Kes ":" The "likes": "PHP", "+": 90} Note:If you do not specify how the sort () method is sorted, the default is in ascending order of the document. From:http://www.runoob.com/mongodb/mongodb-limit-skip.html

mongodb-Foundation-limit-skip-sort

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.