$avg operator - average of salary by group in MongoDB

You are here : Home / MongoDB Tutorial


Contents of page >

$avg operator in MongoDB >
$avg operator returns the average of all numeric values of documents in the collection in MongoDB.
1) First let's create insert documents in collection in MongoDB


1.1) First, Let's create new collection and insert document >
db.employee.insert({_id : 1,  firstName:"ank", salary : 1000 })
db.employee.insert({_id : 2,  firstName:"ank", salary : 2000 })
db.employee.insert({_id : 3,  firstName:"sag", salary : 3000 })
db.employee.insert({_id : 4,  firstName:"sag", salary : 4000 })
db.employee.insert({_id : 5,  firstName:"neh", salary : 5000 })
Above will create collection (or table) (if collection already exists it will insert documents in it).


1.2) FIND > Query all documents of collection using find() method>
db.employee.find()
Output>
{ "_id" : 1, "firstName" : "ank", "salary" : 1000 }
{ "_id" : 2, "firstName" : "ank", "salary" : 2000 }
{ "_id" : 3, "firstName" : "sag", "salary" : 3000 }
{ "_id" : 4, "firstName" : "sag", "salary" : 4000 }
{ "_id" : 5, "firstName" : "neh", "salary" : 5000 }

2) Find average of salary of all employee with same firstName in collection in MongoDB >


Step 2.1 - We will group employee by firstName and find average of salary of each group.
Step 2.2- We will use aggregate() method, $avg operator  and $group operator.
db.employee.aggregate([{$group : {_id : "$firstName", salary_average : {$avg : "$salary"}}}])
Output >
{ "_id" : "ank", "salary_average" : 1500 }
{ "_id" : "sag", "salary_average" : 3500 }
{ "_id" : "neh", "salary_average" : 5000 }


2.3) Sql query equivalent to above MongoDB query is >
select firstName, avg(salary) salary_average from employee group by firstName;


3) Find average of salary of all employee in collection in MongoDB >


Step 3.1 - We will group employee by null (nothing) and find average of salary of group (there will be only 1 group - i.e. whole documents of collection)
Step 3.2- We will use aggregate() method, $group operator, $avg operator and _id : null
db.employee.aggregate([{$group : {_id : null, salary_average : {$avg : "$salary"}}}])
Output >
{ "_id" : null, "salary_average" : 3000 }


3.3) Sql query equivalent to above MongoDB query is >
select avg(salary) salary_average from employee;

4) How to write aggregate query with where clause in MongoDB >


Find average of salary of all employee with same firstName where salary > 2000 in collection in MongoDB >
Step 4.1 - We will find document where salary > 2000
Step 4.2 - We will use $match operator
Step 4.3 - Then we will group employee by firstName and find average of salary of each group.
Step 4.4 - We will use aggregate() method, $group operator, $avg operator.


db.employee.aggregate([
 { $match: { salary : { $gt: 2000} } },
 {$group : {_id : "$firstName", salary_average : {$avg : "$salary"}}}])
Output >
{ "_id" : "sag", "salary_average" : 3500 }
{ "_id" : "neh", "salary_average" : 5000 }


4.5) Sql query equivalent to above MongoDB query is >
select firstName, avg(salary) salary_average from employee
where salary > 2000
group by firstName

5) Summary -
So in this MongoDB tutorial we learned how to use $avg operator in MongoDB. $avg operator returns the average of all numeric values of documents in the collection in MongoDB.


Find average of salary of all employee with same firstName in collection in MongoDB >
db.employee.aggregate([{$group : {_id : "$firstName", salary_average : {$avg : "$salary"}}}])


Find average of salary of all employee in collection in MongoDB >
db.employee.aggregate([{$group : {_id : null, salary_average : {$avg : "$salary"}}}])


How to write aggregate query with where clause in MongoDB >
db.employee.aggregate([
 { $match: { salary : { $gt: 2000} } },
 {$group : {_id : "$firstName", salary_average : {$avg : "$salary"}}}])

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

eEdit
Must read for you :