
- 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 MongoDB Collection by Array value?
To sort MongoDB collection by Array value, use aggregate() along with $sort. Let us create a collection with documents −
> db.demo577.insertOne( ... { ... ... "student": { ... "details": [ ... { ... Name:"Chris", ... Score:45 ... }, ... { ... Name:"Bob", ... Score:33 ... }, ... { ... Name:"David", ... Score:48 ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e916ff1581e9acd78b427ff") }
Display all documents from a collection with the help of find() method −
> db.demo577.find();
This will produce the following output −
{ "_id" : ObjectId("5e916ff1581e9acd78b427ff"), "student" : { "details" : [ { "Name" : "Chris", "Score" : 45 }, { "Name" : "Bob", "Score" : 33 }, { "Name" : "David", "Score" : 48 } ] } }
Following is the query to sort the collection by array value −
> db.demo577.aggregate([ ... {$unwind:"$student"}, ... {$unwind:"$student.details"}, ... ... {$sort:{"student.details.Score":-1}} ... ]);
This will produce the following output −
{ "_id" : ObjectId("5e916ff1581e9acd78b427ff"), "student" : { "details" : { "Name" : "David", "Score" : 48 } } } { "_id" : ObjectId("5e916ff1581e9acd78b427ff"), "student" : { "details" : { "Name" : "Chris", "Score" : 45 } } } { "_id" : ObjectId("5e916ff1581e9acd78b427ff"), "student" : { "details" : { "Name" : "Bob", "Score" : 33 } } }
- Related Articles
- Script to add a single value to array in MongoDB collection?
- MongoDB aggregation framework to sort by length of array?
- Sort collection by multiple fields in Kotlin
- Sort php multidimensional array by sub-value in PHP
- Sort array of objects by string property value - JavaScript
- Matching MongoDB collection items by id?
- Add MD5 hash value to MongoDB collection?
- How to sort by the difference in array contents in MongoDB?
- Sort by subdocument in MongoDB
- Sort array of objects by string property value in JavaScript
- MongoDB query to sort nested array?
- Group by dates in a MongoDB collection?
- How to sort the documents of a MongoDB collection using java?
- MongoDB query to sort by words
- MongoDB to sort by subdocument match?

Advertisements