Learn How to Limit, Skip and Sort Records in MongoDB

0
3272
records

records

In this chapter, we are going to explore limit, skip and sort methods to operate on the documents present inside a collection of MongoDB.

The limit () Method
In MongoDB, the ‘limit ()’ method is used to limit the records or documents present inside a collection. It accepts only one argument which is of number type. Depending on the value of the number, we can limit the number of documents to be displayed. This argument is an optional field inside the ‘limit ()’ method, and when not specified then by default, it will display all the documents from the collection.

The following is the basic syntax for ‘limit ()’ method in MongoDB.

Example
The following is an example on using the ‘limit ()’ statement. Let’s insert 4 documents into a collection known as ‘demo_db’.

Explanation of the code

  • Here, we are using ‘demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘save ()’ method to insert four records together as the bulk write operation in the MongoDB.
  • Next, we are using the ‘find ()’ to display all the saved records.
  • Next, we are using the ‘limit (2)’ method with the argument value as 2 which will limit the display of records to two as shown below.

limit

The skip () Method
In MongoDB, the ‘skip ()’ method is used to skip the number of documents. Like the ‘limit ()’ method, it accepts only one argument which is of number type. Depending on the value of the number, we can skip the number of documents to be displayed. This argument is an optional field inside the ‘skip ()’ method, and when not specified, then it will display all documents from the collection as the default value in ‘skip ()’ method is 0.

The following is the basic syntax for ‘skip ()’ method in the MongoDB.

Example
The following is an example on using the ‘skip ()’ statement.

Explanation of the code

  • Here, we are using ‘demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘find ()’ method to display all the saved documents inside this collection.
  • Next, we are using the ‘limit (2)’ along with ‘skip (1) ‘method to make sure only two documents are displayed after skipping one document as shown below.

skip

The sort () Method
In MongoDB, the ‘sort ()’ method is used to sort the documents inside a collection. It accepts a document which contains a list of fields along with their sorting order. We use 1 and -1 in order to specify the sorting order where 1 corresponds to the ascending order and -1 corresponds to the descending order. Also, it should be noted that if we do not specify any sorting preference, then ‘sort ()’ method will display the documents in the ascending order.

The following is the basic syntax for ‘sort ()’ method in the MongoDB.

Example
The following is an example on using the ‘sort ()’ statement.

Explanation of the code

  • Here, we are using ‘demo_db’ as the collection name of MongoDB.
  • Next, we are using the ‘find ()’ method to display all the saved documents inside this collection.
  • Next, we are using the ‘sort ({“city”: -1})’ method to display the cities in the descending order as the argument value is -1.
  • Lastly, we are using the ‘sort ({“city”: 1})’ method to display the cities in the ascending order as the argument value is 1.

sort

Conclusion
In this chapter, we have learnt about practical use of limit, skip, and sort methods with the help of numerous examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here