Aggregation :
To write aggregate queries , a pipeline operation.
If you have some docs we fired one query in which we group data first
query result is stored in another , then second in another query …etc.
How to achieve : db.collection.aggregate(pipeline,options)
pipeline : array of different operations first match male , group age , sort
options : explain = true ,allowDisk = true (rarely used)
Group :
The $group operator groups document by the age field , creating a
new document for each unique age values.
THE _ID FIELD IN THE GROUP STAGE SPECIFIES THE FIELD BASED ON
WHICH THE DOCUMENTS WILL BE GROUPED.
$ sign denotes age as the field
The names field uses the $push operator to add the same field from each
document in the group to an array
The $$ROOT value is a reference to the current document being
processed in the pipeline , which represents the complete document.
Here Result of male teahcers list is given to next for
group by age and count them using $sum:1
(increment by 1), this is called aggregation pipeline.
The value of $sum is 1 , which means that for each document in the group
, the value of “number” will be incremented by 1
See in 35 age group count to ensure
Query for it :
db.teachers.aggregate([ {$match : {gender :
“male”}} , {$group : {_id:”$age” , count : {$sum :
1}}} , {$sort : {count : -1}} ])
$toDouble operator :
Unwind operator :
It breaks array and create different
document with different values of hobbies
from data.
Answer :
Now no nested array are here
Answer :
db.students.aggregate([ {$unwind :
“$hobbies”} , { $group : {_id:”$hobbies” ,
count : { $sum : “$1”} }} ])
Answer :
db.students.aggregate([{ $group : { _id :
null , avg : { $avg : “$age”}} }])
If you specify _id:null in the $group
operator , it means that all the documents in
the collection will be grouped together into a
single group.
Find Total Number Of hobbies for all the
students in a collection.
db.students.aggregate([ {$unwind:”$hobbies”} ,
{$group : {_id:null , count : {$sum : 1} } } ])
Another way :
Worked but if any one document doesn’t have field hobbies than error will
occur to avoid that error :
ERROR : PlanExecutor error during aggregation
db.students.aggregate([ { $group : { _id:null , count : { $sum : { $size :
{ $ifNull : ["$hobbies",[]]} } } } } ])
Syntax :
{ $size : <expression> }
{ $ifNull : <expression> ,
<replacementExpression> }
Now Display All Hobbies :
db.students.aggregate([ {$unwind : "$hobbies"} , {$group :
{_id:null , allHobbies : { $push : "$hobbies"}}} ])
Here , duplication occur in Hobbies while using $push
Let’s Overcome this problem by using $addToSet
$filter :
Syntax : $filter : {
input:<array expression>,
as : <identifier>,
cond : <expression>
}
Input : Specifies the array expression to filter.
As : Specifies a variable name that can be used inside the cond
expression to reference the current element of the input array.
Cond : Specifies the condition that must be met in order for an element
to be included in the result set. The expression must return either true or
false.
Que : Find Average of score for students whose age is greater than 20
Answer :