
- 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
Aggregation framework to get the name of students with test one score less than the total average of all the tests
For this, you can use aggregate(). We have considered test records as “Value1”, “Value2”, etc. Let us create a collection with documents −
> db.demo432.insertOne( ... { ... "_id" : 101, ... "Name" : "David", ... "Value1" : 67, ... "Value2" : 87, ... "Value3" : 78 ... } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo432.insertOne( ... { ... "_id" : 102, ... "Name" : "Sam", ... "Value1" : 98, ... "Value2" : 45, ... "Value3" : 90 ... } ... ) { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo432.find();
This will produce the following output −
{ "_id" : 101, "Name" : "David", "Value1" : 67, "Value2" : 87, "Value3" : 78 } { "_id" : 102, "Name" : "Sam", "Value1" : 98, "Value2" : 45, "Value3" : 90 }
Following is the query to get the name of students with test one score to be less than average of all the tests −
> db.demo432.aggregate([{ ... $project: { ... Name: '$Name', ... Value1: '$Value1', ... average: { ... $avg: ['$Value1', '$Value2', '$Value3'] ... } ... } ... }, ... { ... $group: { ... _id: null, ... NameValue1: { ... $push: { ... "Name": "$Name", ... "Value1": "$Value1" ... } ... }, ... totalAverage: { ... $avg: '$average' ... } ... } ... }, ... { $project: ... { lessthanAverageNames: ... { ... $map: ... { ... input: { ... $filter: { ... input: "$NameValue1", ... as: "out", ... cond: { ... $lt: ["$$out.Value1", "$totalAverage"] ... } ... } ... }, ... as: "o", ... in: "$$o.Name" ... } ... } ... } ... } ... ]);
This will produce the following output −
{ "_id" : null, "lessthanAverageNames" : [ "David" ] }
- Related Articles
- Get the average of an entire field using the aggregation framework in MongoDB?
- How to sum the score of students with the same name in MySQL with ORDER BY?
- Get Absolute value with MongoDB aggregation framework?
- Find out the records of students with more than a specific score in MySQL?
- Python program to find average score of each students from dictionary of scores
- C++ program to check how many students have greater score than first one
- MongoDB query to get average in aggregation of array element?
- The number of boys in a class is 5 less than twice the number of girls in the class if the total number of boys is 23 find the total number of students in the class
- MongoDB aggregation framework with group query example?
- MongoDB aggregation framework to sort by length of array?
- Find the measure of all the angles of a parallelogram, if one angle is $24^o$ less than twice the smallest angle.
- In a class of 40 students, \( \frac{1}{5} \) of the total number of students like to eat rice only, \( \frac{2}{5} \) of the total number of students like to eat chapati only and the remaining students like to eat both. What fraction of the total number of students like to eat both?
- Shalina got $\frac{4}{5}$ of total marks in a test. If the total marks were 50, How much did she score?
- The average marks of $10$ students is $41$. If the average marks of the top $6$ and bottom $5$ rankers are $45$ and $36$ respectively, how much did the $5^{th}$ ranker score?
- In a college, out of 4320 students, 2300 are girls. Find the ratio of(a) Number of girls to the total number of students.(b) Number of boys to the number of girls.(c) Number of boys to the total number of students.

Advertisements