
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 Articles
- How to do alphanumeric sort in MongoDB?
- How to sort inner array 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?
- How to sort, select and query subdocument in MongoDB?
- How do I sort natural in MongoDB?
- MongoDB query to sort subdocuments
- MongoDB aggregate query to sort
- How to sort by the difference in array contents in MongoDB?
- Perform aggregation sort in MongoDB?
- Sort by subdocument in MongoDB
- MongoDB query to sort by words
- MongoDB to sort by subdocument match?
- MongoDB query to sort nested array?

Advertisements