After MongoDB uses the find() method to query, it may be necessary to sort the returned results. In some queries, we do not need to return all results, but a specified number of results with a specified page length and page number

  1. Limit () Specifies the number of query results

  2. Skip () specifies the query offset

  3. Sort () implements sorting of query results

  4. Limit () Specifies the number of query results

The find() method, after querying a document, returns the first 20 pieces of data by default. To limit the number of MongoDB query results, use the limit() method, which accepts a numeric value for the number of results to be returned.

Limit () syntax construction

db.COLLECTION_NAME.find().limit(NUMBER)
Copy the code

For example, the existing Sites data collection has the following collection data:

{ "_id" : ObjectId("56999ca102129bdec412ed38"), "name" : "IT record"."domain" : "itbilu.com" }
{ "_id" : ObjectId("56999ca902129bdec412ed39"), "name" : "Mugu)"."domain" : "niefengjun.cn" }
{ "_id" : ObjectId("56999cb102129bdec412ed3a"), "name" : "A piece of cloth"."domain" : "yijiebuyi.com" }
Copy the code

Query the collection and return only the first data:

> db.sites.find().limit(1)
{ "_id" : ObjectId("56999ca102129bdec412ed38"), "name" : "IT record"."domain" : "itbilu.com" }
Copy the code
  1. Skip () specifies the query offset

In databases that use SQL queries (such as mySQL and PostgreSQL), you can use limit quantity, offset, or limit quantity offset to perform paging queries. In MongoDB, you can specify offsets using the skip() method, which also takes a quantity parameter.

Skip () syntax structure

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Copy the code

For example, use skip() to query the second document in the Sites collection:

> db.sites.find().limit(1).skip(1)
{ "_id" : ObjectId("56999ca902129bdec412ed39"), "name" : "Mugu)"."domain" : "niefengjun.cn" }
Copy the code
  1. Sort () implements sorting of query results

To sort MongoDB query documents, use the sort() method. The sort() method takes a document containing the sorted fields and the sorting method to specify. Sorting mode Possible values are 1 and -1. 1 indicates ascending sorting, and -1 indicates descending sorting.

Sort () syntax structure

db.COLLECTION_NAME.find().sort({KEY:1})
Copy the code

For example, to query the Sites data set, try the ascending and descending order of name respectively:

> db.sites.find().sort({name:1{})"_id" : ObjectId("56999ca102129bdec412ed38"), "name" : "IT record"."domain" : "itbilu.com" }
{ "_id" : ObjectId("56999cb102129bdec412ed3a"), "name" : "A piece of cloth"."domain" : "yijiebuyi.com" }
{ "_id" : ObjectId("56999ca902129bdec412ed39"), "name" : "Mugu)"."domain" : "niefengjun.cn" }
> db.sites.find().sort({name: -1{})"_id" : ObjectId("56999ca902129bdec412ed39"), "name" : "Mugu)"."domain" : "niefengjun.cn" }
{ "_id" : ObjectId("56999cb102129bdec412ed3a"), "name" : "A piece of cloth"."domain" : "yijiebuyi.com" }
{ "_id" : ObjectId("56999ca102129bdec412ed38"), "name" : "IT record"."domain" : "itbilu.com" }
Copy the code