0% found this document useful (0 votes)
36 views

Itvoyagers Aggregation in Mongodb

The document discusses various MongoDB aggregation operations like $sum, $avg, $min, $max, $push, $addToSet, $first, and $last. It provides examples of queries using each of these operations on a sample student collection. For operations like $sum, $avg, $min, $max, it groups documents by name and performs the respective aggregation. For $push and $addToSet, it adds arrays to documents. And for $first and $last, it retrieves the first and last document marks respectively, grouped by name or subject.

Uploaded by

k.nitin.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Itvoyagers Aggregation in Mongodb

The document discusses various MongoDB aggregation operations like $sum, $avg, $min, $max, $push, $addToSet, $first, and $last. It provides examples of queries using each of these operations on a sample student collection. For operations like $sum, $avg, $min, $max, it groups documents by name and performs the respective aggregation. For $push and $addToSet, it adds arrays to documents. And for $first and $last, it retrieves the first and last document marks respectively, grouped by name or subject.

Uploaded by

k.nitin.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ITVoyagers (itvoyagers.

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…”

Type a name for the collection.

Click on “Create”. Once collection is created now let’s insert few


documents in this collection.

1
ITVoyagers (itvoyagers.in)
Type document in this window

Following is our first document.

Once the documents is written click on “Save” and few more document
using same process. Let’s view document.

2
ITVoyagers (itvoyagers.in)

We have inserted total 8 documents in collection.

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.

Write a MongoDB query to use sum, avg, min and


max expression.
In mongoDB aggregate method we will be using $match, $group and $sort
as it parameter.
$match : Allow us to set the condition on basis of which mongoDB will
filter the data.
$group : It has “_id” and “total” by using which we can perform aggregate
operations.
$sort : It is use to order the data in ascending or descending order.
Let’s start

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:

If we want result to be displayed in descending order, we have to add


$sort in our query in $sort we have we have to set “total” to -1.
-1 represents descending order.

5
ITVoyagers (itvoyagers.in)

Output:

If we want result to be displayed in Ascending order, we have to add


$sort in our query in $sort we have we have to set “total” to 1.
1 represents descending order.

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)

Write a MongoDB query to use push and addToSet


expression.
This is our collection in tabular format, let’s view our collection in JSON
format by clicking on “View result on text mode”.

We are going to perform push and addToSet operations on first document


i.e. document which has “name” : “Vijay”.

$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:

Write a MongoDB query to use first and last


expression.
Just view our student collection documents

This is our collection

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.

We can use $first to get those marks.


Now in below operations we will use $group and $first expression.
$group : In $group we will set “_id” to “$name” which states that we will
we performing operation on “name” key of all documents.
$first : In we will pass “$marks” which states that it will retrieve first
document mark from documents which has same “name” value.
And first_mark is nothing but the column name in which result is going to
be displayed.

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

You might also like