
- 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
Sort the MongoDB documents in ascending order with aggregation?
Use $sort in MongoDB aggregation. Let us create a collection with documents −
> db.demo652.insertOne({ ... value:10, ... "details" : [{ ... "ProductName" : "Product-1", ... "ProductQuantity" : 8, ... "ProductPrice" : 500 ... }, { ... "ProductName" : "Product-2", ... "ProductQuantity" : 7, ... "ProductPrice" : 500 ... ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9f0730e3c3cd0dcff36a62") } > > db.demo652.insertOne({ ... value:5, ... "details" : [{ ... "ProductName" : "Product-1", ... "ProductQuantity" : 8, ... "ProductPrice" : 500 ... }, { ... "ProductName" : "Product-2", ... "ProductQuantity" : 7, ... "ProductPrice" : 500 ... ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9f0740e3c3cd0dcff36a63") }
Display all documents from a collection with the help of find() method −
> db.demo652.find();
This will produce the following output −
{ "_id" : ObjectId("5e9f0730e3c3cd0dcff36a62"), "value" : 10, "details" : [ { "ProductName" : "Product-1", "ProductQuantity" : 8, "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductQuantity" : 7, "ProductPrice" : 500 } ] } { "_id" : ObjectId("5e9f0740e3c3cd0dcff36a63"), "value" : 5, "details" : [ { "ProductName" : "Product-1", "ProductQuantity" : 8, "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductQuantity" : 7, "ProductPrice" : 500 } ] }
Following is the query to sort in ascending order −
> db.demo652.aggregate([{ ... "$unwind": "$details" ... }, { ... "$match": {} ... }, { ... "$group": { ... "ProductPrice": { ... "$first": "$value" ... }, ... "details": { ... "$push": { ... "ProductName": "$details.ProductName" ... } ... }, ... "_id": "$_id" ... } ... }, { ... "$sort": { ... "ProductPrice": 1 ... } ... }, { ... "$project": { ... "_id": 0, ... "ProductPrice": 1, ... "details": 1 ... } ... }]).pretty()
This will produce the following output −
{ "ProductPrice" : 5, "details" : [ { "ProductName" : "Product-1" }, { "ProductName" : "Product-2" } ] } { "ProductPrice" : 10, "details" : [ { "ProductName" : "Product-1" }, { "ProductName" : "Product-2" } ] }
- Related Articles
- Sort MongoDB documents in descending order
- How to perform ascending order sort in MongoDB?
- Aggregation in MongoDB for nested documents?
- MongoDB aggregation to get two documents with the least marks
- Perform aggregation sort in MongoDB?
- MongoDB aggregation of elements with similar ids in different documents?
- MongoDB aggregation to fetch documents with specific field value?
- Sort index in ascending order – Python Pandas
- Sort and Group in one MongoDB aggregation query?
- How to sort Java array elements in ascending order?
- How can we sort MySQL output in ascending order?
- 8085 Program to perform bubble sort in ascending order
- 8085 Program to perform selection sort in ascending order
- 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