
- 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 by subdocument in MongoDB
To sort by subdocument, use $sort in MongoDB. Let us create a collection with documents −
> db.demo245.insertOne( ... { ... "_id": 101, ... "deatils": [ ... { "DueDate": new ISODate("2019-01-10"), "Value": 45}, ... {"DueDate": new ISODate("2019-11-10"), "Value": 34 } ... ] ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo245.insertOne( ... { ... "_id": 102, ... "details": [ ... { "DueDate": new ISODate("2019-12-11"), "Value": 29}, ... {"DueDate": new ISODate("2019-03-10"), "Value": 78} ... ] ... } ...); { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo245.find();
This will produce the following output −
{ "_id" : 101, "deatils" : [ { "DueDate" : ISODate("2019-01-10T00:00:00Z"), "Value" : 45 }, { "DueDate" : ISODate("2019-11-10T00:00:00Z"), "Value" : 34 } ] } { "_id" : 102, "details" : [ { "DueDate" : ISODate("2019-12-11T00:00:00Z"), "Value" : 29 }, { "DueDate" : ISODate("2019-03-10T00:00:00Z"), "Value" : 78 } \ ] }
Following is the query to sort by subdocument −
> db.demo245.aggregate([ ... { "$unwind": "$details" }, ... { "$sort": { "_id": 1, "details.Value": -1 } }, ... { "$group": { ... "_id": "$_id", ... "details": { "$push": "$details" } ... }}, ... { "$sort": { "details.Value": -1 } } ...])
This will produce the following output −
{ "_id" : 102, "details" : [ { "DueDate" : ISODate("2019-03-10T00:00:00Z"), "Value" : 78 }, { "DueDate" : ISODate("2019-12-11T00:00:00Z"), "Value" : 29 } ] }
- Related Articles
- MongoDB to sort by subdocument match?
- How to sort, select and query subdocument in MongoDB?
- How to query documents by a condition on the subdocument in MongoDB?
- How to select a specific subdocument in MongoDB?
- Finding a specific item in subdocument with MongoDB?
- How to filter array in subdocument with MongoDB?
- MongoDB query to remove subdocument from document?
- Can I utilize indexes when querying by MongoDB subdocument without known field names?
- MongoDB query to sort by words
- Sort MongoDB Collection by Array value?
- Query MongoDB subdocument to print on one line?
- Push new key element into subdocument of MongoDB?
- MongoDB query to find and return subdocument with criteria?
- Display MongoDB with document and subdocument example and update
- MongoDB query on nth element (variable index) of subdocument array

Advertisements