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”
db.students.find() testset:PRIMARY>db.students.find() { "_id" : ObjectId("59245acd5273c93ad95b109b"), "firstname" : "Maria", "city" : "Banglore", "age" : 21 } { "_id" : ObjectId("59246d4b5273c93ad95b109c"), "firstname" : "sneha", "city" : "Delhi", "age" : 21 } { "_id" : ObjectId("59246d7b5273c93ad95b109d"), "firstname" : "jay", "city" : "toronto", "age" : 21 } { "_id" : ObjectId("59254ec6c2c4480ef9a1e5bb"), "firstname" : "harry", "city" : "texas", "age" : 24 } { "_id" : ObjectId("59254f0cc2c4480ef9a1e5bc"), "firstname" : "rita", "city" : "california", "age" : 22 } { "_id" : ObjectId("59254f2dc2c4480ef9a1e5bd"), "firstname" : "prasad", "city" : "newyork", "age" : 26 } { "_id" : ObjectId("59254f47c2c4480ef9a1e5be"), "firstname" : "devi", "city" : "ohio", "age" : 26 }
Limit the data you want as result
db.students.find().limit(5).pretty();
testset:PRIMARY>db.students.find().limit(5).pretty(); { "_id" : ObjectId("59245acd5273c93ad95b109b"), "firstname" : "Maria", "city" : "Banglore", "age" : 21 } { "_id" : ObjectId("59246d4b5273c93ad95b109c"), "firstname" : "sneha", "city" : "Delhi", "age" : 21 } { "_id" : ObjectId("59246d7b5273c93ad95b109d"), "firstname" : "jay", "city" : "toronto", "age" : 21 } { "_id" : ObjectId("59254ec6c2c4480ef9a1e5bb"), "firstname" : "harry", "city" : "texas", "age" : 24 } { "_id" : ObjectId("59254f0cc2c4480ef9a1e5bc"), "firstname" : "rita", "city" : "california", "age" : 22 }
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});
testset:PRIMARY>db.students.find().sort({"firstname":1}); { "_id" : ObjectId("59245acd5273c93ad95b109b"), "firstname" : "Maria", "city" : "Banglore", "age" : 21 } { "_id" : ObjectId("59254f47c2c4480ef9a1e5be"), "firstname" : "devi", "city" : "ohio", "age" : 26 } { "_id" : ObjectId("59254ec6c2c4480ef9a1e5bb"), "firstname" : "harry", "city" : "texas", "age" : 24 } { "_id" : ObjectId("59246d7b5273c93ad95b109d"), "firstname" : "jay", "city" : "toronto", "age" : 21 } { "_id" : ObjectId("59254f2dc2c4480ef9a1e5bd"), "firstname" : "prasad", "city" : "newyork", "age" : 26 } { "_id" : ObjectId("59254f0cc2c4480ef9a1e5bc"), "firstname" : "rita", "city" : "california", "age" : 22 } { "_id" : ObjectId("59246d4b5273c93ad95b109c"), "firstname" : "sneha", "city" : "Delhi", "age" : 21 }
Sort the data in descending order as result
db.students.find().sort({“firstname”:-1});
testset:PRIMARY>db.students.find().sort({"firstname":-1}); { "_id" : ObjectId("59246d4b5273c93ad95b109c"), "firstname" : "sneha", "city" : "Delhi", "age" : 21 } { "_id" : ObjectId("59254f0cc2c4480ef9a1e5bc"), "firstname" : "rita", "city" : "california", "age" : 22 } { "_id" : ObjectId("59254f2dc2c4480ef9a1e5bd"), "firstname" : "prasad", "city" : "newyork", "age" : 26 } { "_id" : ObjectId("59246d7b5273c93ad95b109d"), "firstname" : "jay", "city" : "toronto", "age" : 21 } { "_id" : ObjectId("59254ec6c2c4480ef9a1e5bb"), "firstname" : "harry", "city" : "texas", "age" : 24 } { "_id" : ObjectId("59254f47c2c4480ef9a1e5be"), "firstname" : "devi", "city" : "ohio", "age" : 26 } { "_id" : ObjectId("59245acd5273c93ad95b109b"), "firstname" : "Maria", "city" : "Banglore", "age" : 21 }