
- 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 use MongoDB Aggregate to sort?
Use aggregate() and within that to sort, use $sort in MongoDB. Let us create a collection with documents −
> db.demo164.insertOne({"StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e36883d9e4f06af551997c8") } > db.demo164.insertOne({"StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5e3688409e4f06af551997c9") } > db.demo164.insertOne({"StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3688429e4f06af551997ca") } > db.demo164.insertOne({"StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e3688439e4f06af551997cb") }
Display all documents from a collection with the help of find() method −
> db.demo164.find();
This will produce the following output −
{ "_id" : ObjectId("5e36883d9e4f06af551997c8"), "StudentAge" : 24 } { "_id" : ObjectId("5e3688409e4f06af551997c9"), "StudentAge" : 25 } { "_id" : ObjectId("5e3688429e4f06af551997ca"), "StudentAge" : 22 } { "_id" : ObjectId("5e3688439e4f06af551997cb"), "StudentAge" : 21 }
Here is the query to use MongoDB aggregate() −
> db.demo164.aggregate([{ $sort : { StudentAge : -1 } }]);
This will produce the following output −
{ "_id" : ObjectId("5e3688409e4f06af551997c9"), "StudentAge" : 25 } { "_id" : ObjectId("5e36883d9e4f06af551997c8"), "StudentAge" : 24 } { "_id" : ObjectId("5e3688429e4f06af551997ca"), "StudentAge" : 22 } { "_id" : ObjectId("5e3688439e4f06af551997cb"), "StudentAge" : 21 }
- Related Articles
- MongoDB aggregate query to sort
- How to update after aggregate in MongoDB?
- How to aggregate array documents in MongoDB?
- How to calculate sum in MongoDB with aggregate()?
- How to use Oracle aggregate function XMLAGG ?
- MongoDB query to implement aggregate function
- MongoDB query to aggregate nested array
- How to sort in MongoDB?
- Use MongoDB Aggregate and select only top record (descending)
- How to aggregate sum in MongoDB to get the total count?
- How to get only values in arrays with MongoDB aggregate?
- To Aggregate Totals in One Group with MongoDB
- MongoDB Aggregate to limit the number of records
- How to do alphanumeric sort in MongoDB?
- How to sort inner array in MongoDB?

Advertisements