
- 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 perform ascending order sort in MongoDB?
To sort in ascending 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 }
Here is the query to get the 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 }
- Related Articles
- How to perform descending order sort in MongoDB?
- 8085 Program to perform bubble sort in ascending order
- 8085 Program to perform selection sort in ascending order
- Program to perform bubble sort in ascending order in 8085 Microprocessor
- Program to perform selection sort in ascending order in 8085 Microprocessor
- Sort the MongoDB documents in ascending order with aggregation?
- How to sort Java array elements in ascending order?
- How to sort an ArrayList in Java in ascending order?
- How to sort an ArrayList in Ascending Order in Java
- How can we sort MySQL output in ascending order?
- Sort index in ascending order – Python Pandas
- Perform aggregation sort in MongoDB?
- How to sort bars in a bar plot in ascending order (Matplotlib)?
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Swift Program to sort an array in ascending order using bubble sort

Advertisements