MongoDB Sort Records - MongoDB

How to sort records in MongoDB?

In this chapter, we will explain you how to sort records in MongoDB.

sort() Method

Sort() method is used to sort documents in MongoDB. This method will accept a document which consists of a list of fields including their sorting order. 1 and -1 are used for specifying the sorting order. 1 is used for ascending order and -1 is used for descending order.


Basic syntax of sort() method is as follows


Consider collection mycol has below data.

Below example displays the documents sorted by title in the descending order.

Remember that if you don't specify the sorting preference, then sort() method will display the documents in ascending order.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

MongoDB Topics