Cainiao's mongoDB learning --- (5) MongoDB's limit, skip, and sort methods, mongodblimit

Source: Internet
Author: User

Cainiao's mongoDB learning --- (5) MongoDB's limit, skip, and sort methods, mongodblimit

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.

Mongo # Go To mongoDB
 
<Span style = "line-height: 16.363636016845703px;"> use NewsDB # select NewsDB </span>
 
Db. new. find (). limit (5) # list the first five documents

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


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.

Db. new. find (). skip (5). limit (5) # skip the first five documents and list 6-10 documents.

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


Sort Method

In MongoDB, the sort () method is used to sort data. The sort () method can specify the sorting fields through parameters and specify the sorting method using 1 and-1, 1 indicates the ascending order, and-1 indicates the descending order.

Db. new. find ({}, {"title": 1, _ id: 0 }). sort ({"title":-1}) # only display the title attribute and display it in descending order








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.