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