MongoDB – Limit and Sort:

Limit:

by using the Limit modifier, we can limit the number of documents that can be returned in a query.

Example:

First check the data inside the collection “student”

Limit the data you want as result
db.students.find().limit(5).pretty();

Sort Order:

By using the sort modifier, we can sort the result in a ascending order or descending order.

Example:

Sort the data in ascending order as result
db.students.find().sort({“firstname”:1});

Sort the data in descending order as result
db.students.find().sort({“firstname”:-1});