Itvoyagers Aggregation in Mongodb
Itvoyagers Aggregation in Mongodb
in)
Implementing Aggregation
Let’s create simple collection for student.
To understand the process of Robo 3T installation and connection process
follow the document from this link
Let’s us create new collection for this practical. Right click on “Collections”
and select “Create Collection…”
1
ITVoyagers (itvoyagers.in)
Type document in this window
Once the documents is written click on “Save” and few more document
using same process. Let’s view document.
2
ITVoyagers (itvoyagers.in)
To view all documents from the collection in different and more clear
view click on “View result in table mode” button.
3
ITVoyagers (itvoyagers.in)
Now let’s start performing following functions.
1. sum
Suppose we need to add marks of both the subjects for each student in
collection.
So we will set _id: “$name” which states that we will be grouping using
“name” from each document.
And total : {$sum : “$marks” } which state that we need sum of all
“marks” which has same “name” value.
4
ITVoyagers (itvoyagers.in)
Output:
2. avg
Suppose we need average marks of both the subjects for each student
in collection.
So we will set _id: “$name” which states that we will be grouping using
“name” from each document.
And total : {$avg : “$marks” } which state that we need average of all
“marks” which has same “name” value.
Output:
5
ITVoyagers (itvoyagers.in)
Output:
Output:
3. min
Suppose we need minimum value from marks of each student in
collection.
So we will set _id: “$name” which states that we will be grouping using
“name” from each document.
6
ITVoyagers (itvoyagers.in)
And total : {$min : “$marks” } which state that we need minimum value
of all “marks” which has same “name” value.
Output:
4. max
Suppose we need maximum value from marks of each student in
collection.
So we will set _id: “$name” which states that we will be grouping using
“name” from each document.
And total : {$max : “$marks” } which state that we need maximum
value of all “marks” which has same “name” value.
Output:
7
ITVoyagers (itvoyagers.in)
$push expression
$push is use with update method, it is use to add array element in the
document.
In our example we are going to add “other_subjects” array as element in
our first document which has “name” : “Vijay”. In first parameter of
update method we will set {“name” : “Vijay”} which is nothing but the
condition on basis of which mongoDB will search document. We will add
two key:values pair in “other_subjects” array, execute the command.
8
ITVoyagers (itvoyagers.in)
If we check all document from “student” collection, we will see that new
array has been added in first document which has “name” : “Vijay”.
Let’s try to add one more key:value pair in same array, we have just
added “HTML” : 100 in update command. Execute the command.
And now if we view the document we will see that instead of adding
“HTML” : 100 as element in “other_subjects” array mongoDB has added
all three key:value pairs as another element in “other_subjects” array.
9
ITVoyagers (itvoyagers.in)
This is how $push works it will add array element in document and if
array element is already present then it will append all the values as
separate element in it.
$addToSet expression
$addToSet will add array element in document, but unlike $push if the
array is already present in document then it will update in array and add
the value in array.
In our example we are going to add “theory_subject” array as element in
our first document which has “name” : “Vijay”. In first parameter of
update method we will set {“name” : “Vijay”} which is nothing but the
condition on basis of which mongoDB will search document. We will add
one value in “theory_subject” array, execute the command.
Output:
10
ITVoyagers (itvoyagers.in)
If we check all document from “student” collection, we will see that new
array has been added in first document which has “name” : “Vijay”.
Let’s try adding one more value in same array, we have just change “TOC”
to “SE” in update command. Execute the command.
Output:
We can see that now in “theory_subject” array another element has been
added, unlike $push which adds all the value as separate element.
$addToSet will add array element in document and if it is present then it
will update it, Now let’s add one more array “math_subject” in same
document.
11
ITVoyagers (itvoyagers.in)
Output:
12
ITVoyagers (itvoyagers.in)
$first expression
Now we know that each student has 2 document one for subject python
and another one for C++. We want first document’s marks of all the
students. We want those highlighted which are first entry with respect to
“name” in all documents.
13
ITVoyagers (itvoyagers.in)
Output:
$last expression
As the name suggest it will retrieve last document with respect to search
condition. In our case it will retrieve last document mark which has same
“name” value. Those are the last marks with respect to “name”.
There is only one change in query i.e. instead of $first we will use $last.
14
ITVoyagers (itvoyagers.in)
Output:
Let’s fetch first marks with respect to subject, so that we will be getting
those marks in return, because those are the first entry with respect to
subject i.e. python and c++.
We will set _id: “$subject” and we will use $first to get our result.
Output:
15