

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }
- Related Questions & Answers
- How to sort inner array in MongoDB?
- How to do alphanumeric sort in MongoDB?
- How to use MongoDB Aggregate to sort?
- How to perform ascending order sort in MongoDB?
- How to perform descending order sort in MongoDB?
- MongoDB aggregate query to sort
- MongoDB query to sort subdocuments
- How do I sort natural in MongoDB?
- How to sort, select and query subdocument in MongoDB?
- Perform aggregation sort in MongoDB?
- Sort by subdocument in MongoDB
- MongoDB to sort by subdocument match?
- MongoDB query to sort by words
- MongoDB query to sort nested array?
- How to sort by the difference in array contents in MongoDB?
Advertisements