
- 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
MongoDB query to sort nested array?
To sort nested array in MongoDB, use $sort. Let us create a collection with documents −
> db.demo505.insertOne( ... { ... "details": [ ... { ... Name:"Chris", ... "Score":58 ... }, { ... ... Name:"Bob", ... "Score":45 ... }, { ... ... Name:"John", ... "Score":68 ... }, { ... ... Name:"David", ... "Score":46 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e882a6b987b6e0e9d18f574") }
Display all documents from a collection with the help of find() method −
> db.demo505.find();
This will produce the following output −
{ "_id" : ObjectId("5e882a6b987b6e0e9d18f574"), "details" : [ { "Name" : "Chris", "Score" : 58 }, { "Name" : "Bob", "Score" : 45 }, { "Name" : "John", "Score" : 68 }, { "Name" : "David", "Score" : 46 } ] }
Following is the query to sort nested array −
> db.demo505.aggregate([ ... { $unwind: "$details" }, ... { $sort: { "details.Score": 1 } }, ... { $group: { _id: "$_id", details: { $push: "$details" } } } ... ]);
This will produce the following output −
{ "_id" : ObjectId("5e882a6b987b6e0e9d18f574"), "details" : [ { "Name" : "Bob", "Score" : 45 }, { "Name" : "David", "Score" : 46 }, { "Name" : "Chris", "Score" : 58 }, { "Name" : "John", "Score" : 68 } ] }
- Related Articles
- MongoDB query to aggregate nested array
- MongoDB query to get array of nested string?
- Query array of nested string with MongoDB?
- Query a nested field within an array with MongoDB
- MongoDB query to update nested document
- MongoDB query to get only specific fields in nested array documents?
- Query nested array by more than one condition in MongoDB
- MongoDB query to sort subdocuments
- MongoDB aggregate query to sort
- MongoDB query to update the nested document?
- Sort array in MongoDB query and project all fields?
- MongoDB query to sort by words
- Query MongoDB for a nested search
- Query deeply nested Objects in MongoDB
- MongoDB find() query for nested document?

Advertisements