How to sort in MongoDB?

To sort in MongoDB, you can use the sort() method.
Case 1 − Sort in ascending order. The syntax is as follows −
db.yourCollectionName.find().sort({yourField:1});
Case 2 − Sort in descending order. The syntax is as follows −
db.yourCollectionName.find().sort({yourField:-1});
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.sortingDemo.insertOne({"Value":100});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006")
}
> db.sortingDemo.insertOne({"Value":1});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007")
}
> db.sortingDemo.insertOne({"Value":150});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008")
}
> db.sortingDemo.insertOne({"Value":250});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e37d3c9d04998abf009")
}
> db.sortingDemo.insertOne({"Value":5});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e3bd3c9d04998abf00a")
}
> db.sortingDemo.insertOne({"Value":199});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e3ed3c9d04998abf00b")
}
> db.sortingDemo.insertOne({"Value":243});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e44d3c9d04998abf00c")
}
> db.sortingDemo.insertOne({"Value":290});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8f8e48d3c9d04998abf00d")
}
Display all documents from a collection with the help of find() method. The query is as follows −
> db.sortingDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8f8e2ed3c9d04998abf006"), "Value" : 100 }
{ "_id" : ObjectId("5c8f8e31d3c9d04998abf007"), "Value" : 1 }
{ "_id" : ObjectId("5c8f8e34d3c9d04998abf008"), "Value" : 150 }
{ "_id" : ObjectId("5c8f8e37d3c9d04998abf009"), "Value" : 250 }
{ "_id" : ObjectId("5c8f8e3bd3c9d04998abf00a"), "Value" : 5 }
{ "_id" : ObjectId("5c8f8e3ed3c9d04998abf00b"), "Value" : 199 }
{ "_id" : ObjectId("5c8f8e44d3c9d04998abf00c"), "Value" : 243 }
{ "_id" : ObjectId("5c8f8e48d3c9d04998abf00d"), "Value" : 290 }
Case 1 − Here is the query to get a result in ascending order
> db.sortingDemo.find().sort({Value:1});
The following is the output −
{ "_id" : ObjectId("5c8f8e31d3c9d04998abf007"), "Value" : 1 }
{ "_id" : ObjectId("5c8f8e3bd3c9d04998abf00a"), "Value" : 5 }
{ "_id" : ObjectId("5c8f8e2ed3c9d04998abf006"), "Value" : 100 }
{ "_id" : ObjectId("5c8f8e34d3c9d04998abf008"), "Value" : 150 }
{ "_id" : ObjectId("5c8f8e3ed3c9d04998abf00b"), "Value" : 199 }
{ "_id" : ObjectId("5c8f8e44d3c9d04998abf00c"), "Value" : 243 }
{ "_id" : ObjectId("5c8f8e37d3c9d04998abf009"), "Value" : 250 }
{ "_id" : ObjectId("5c8f8e48d3c9d04998abf00d"), "Value" : 290 }
Case 2 − Here is the query to get a result in descending order. The query is as follows −
> db.sortingDemo.find().sort({Value:-1});
The following is the output −
{ "_id" : ObjectId("5c8f8e48d3c9d04998abf00d"), "Value" : 290 }
{ "_id" : ObjectId("5c8f8e37d3c9d04998abf009"), "Value" : 250 }
{ "_id" : ObjectId("5c8f8e44d3c9d04998abf00c"), "Value" : 243 }
{ "_id" : ObjectId("5c8f8e3ed3c9d04998abf00b"), "Value" : 199 }
{ "_id" : ObjectId("5c8f8e34d3c9d04998abf008"), "Value" : 150 }
{ "_id" : ObjectId("5c8f8e2ed3c9d04998abf006"), "Value" : 100 }
{ "_id" : ObjectId("5c8f8e3bd3c9d04998abf00a"), "Value" : 5 }
{ "_id" : ObjectId("5c8f8e31d3c9d04998abf007"), "Value" : 1 }