
- 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
Finding highest value from sub-arrays in MongoDB documents?
To find the highest value from sub-arrays in documents, you can use an aggregate framework. Let us first create a collection with documents
> db.findHighestValueDemo.insertOne( ... { ... _id: 10001, ... "StudentDetails": [ ... { "StudentName": "Chris", "StudentMathScore": 56}, ... { "StudentName": "Robert", "StudentMathScore":47 }, ... { "StudentName": "John", "StudentMathScore": 98 }] ... } ... ); { "acknowledged" : true, "insertedId" : 10001 } > db.findHighestValueDemo.insertOne( ... { ... _id: 10002, ... "StudentDetails": [ ... { "StudentName": "Ramit", "StudentMathScore": 89}, ... { "StudentName": "David", "StudentMathScore":76 }, ... { "StudentName": "Bob", "StudentMathScore": 97 } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 10002 }
Following is the query to display all documents from a collection with the help of find() method
> db.findHighestValueDemo.find().pretty();
This will produce the following output
{ "_id" : 10001, "StudentDetails" : [ { "StudentName" : "Chris", "StudentMathScore" : 56 }, { "StudentName" : "Robert", "StudentMathScore" : 47 }, { "StudentName" : "John", "StudentMathScore" : 98 } ] } { "_id" : 10002, "StudentDetails" : [ { "StudentName" : "Ramit", "StudentMathScore" : 89 }, { "StudentName" : "David", "StudentMathScore" : 76 }, { "StudentName" : "Bob", "StudentMathScore" : 97 } ] }
Following is the query to find the highest value from sub-arrays in documents
> db.findHighestValueDemo.aggregate([ ... {$project:{"StudentDetails.StudentName":1, "StudentDetails.StudentMathScore":1}}, ... {$unwind:"$StudentDetails"}, ... {$sort:{"StudentDetails.StudentMathScore":-1}}, ... {$limit:1} ... ]).pretty();
This will produce the following output
{ "_id" : 10001, "StudentDetails" : { "StudentName" : "John", "StudentMathScore" : 98 } }
- Related Articles
- Filter sub documents by sub document in MongoDB?
- Sum MongoDB Sub-documents field?
- MongoDB filter multiple sub-documents?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- How would I filter out sub documents in MongoDB?
- Search for documents with similar arrays in MongoDB and order by similarity value
- Finding documents in MongoDB collection where a field is equal to given integer value?
- How to fire find query on sub-documents in MongoDB?
- Projection of arrays to get the first array element from MongoDB documents
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Find a value in lowercase from a MongoDB collection with documents
- Find value above a specific value in MongoDB documents?
- Getting the highest value of a column in MongoDB?
- MongoDB, finding all documents where property id equals to record id?
- Updating a set of documents from a list of key value pairs in MongoDB

Advertisements