Wednesday, April 30, 2014

MongoDB Aggregation

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. In sql count(*) and with group by is an equivalent of mongodb aggregation.

Creating new collections for MongoDB Aggregation

Here I’m inserting  documents(rows) into Student collection(Table)

db.Student.insert ({Student_Name:"Kalki",  Class: "2", Mark_Scored:100, Subject: ["Telugu", "English", "Maths"]})
db.Student.insert ({Student_Name:"Matsya", Class: "1", Mark_Scored:10,  Subject: ["Telugu", "English"]})

db.Student.insert ({Student_Name:"Krishna",Class: "1", Mark_Scored:50,  Subject: ["Telugu"]})
db.Student.insert ({Student_Name:"Buddha", Class: "2", Mark_Scored:60,  Subject: ["Telugu"]})
db.Student.insert ({Student_Name:"Rama",   Class: "2", Mark_Scored:80,  Subject: ["Telugu"]})

db.Student.insert ({Student_Name:"Krishna",Class: "1", Mark_Scored:50,  Subject: ["English"]})
db.Student.insert ({Student_Name:"Buddha", Class: "2", Mark_Scored:60,  Subject: ["English"]})
db.Student.insert ({Student_Name:"Rama",   Class: "2", Mark_Scored:80,  Subject: ["English"]})

db.Student.insert ({Student_Name:"Matsya", Class: "1", Mark_Scored:67,  Subject: ["Maths"]})
db.Student.insert ({Student_Name:"Krishna",Class: "1", Mark_Scored:95,  Subject: ["Maths"]})
db.Student.insert ({Student_Name:"Buddha", Class: "2", Mark_Scored:88,  Subject: ["Maths"]})
db.Student.insert ({Student_Name:"Rama",   Class: "2", Mark_Scored:40,  Subject: ["Maths"]})
Inserting and finding documents from collection

$sum:-
Sums up the defined value from all documents in the collection.
>db.Student.aggregate([{$group : {_id : "$item", num_tutorial : {$sum : "$Mark_Scored" }}}])

Ex: Lets try and get the sum of all the marks scored by each and every student, in Class "2"
db.Student.aggregate ([
   {
      "$match":
      {
         "Class": "2"
      }
   },
   {
      "$unwind": "$Subject"
   },
   {
      "$group":
      {
         "_id":
         {
            "Student_Name" : "$Student_Name"
         },
         "Total_Marks":
         {
            "$sum": "$Mark_Scored"
         }
      }
   }
])



$avg:-
> db.Student.aggregate([{$group : {_id : "$item", num_tutorial : {$avg : "$Mark_Scored" }}}])

Ex:
db.Student.aggregate ([
   {
      "$match":
      {
         "Class": "2"
      }
   },
   {
      "$unwind": "$Subject"
   },
   {
      "$group":
      {
         "_id":
         {
            "Student_Name" : "$Student_Name"
         },
         "Total_Marks":
         {
            "$sum": "$Mark_Scored"
         }
      }
   }
])



$match

Example:We want to consider only the marks of the students who study in Class "2"

db.Student.aggregate ([
  {
     "$match":
     {
        "Class":"2"
     }
  }
  ])

And results is




Let us say, we want to consider only the marks of the students who study in Class "2" and whose marks are more than or equal to 80
db.Student.aggregate ([
  {
     "$match":
     {
        "Class":"2",
        "Mark_Scored":
        {
           "$gte": 80
        }
     }
  }
  ])
And results is

$unwind


This will be very useful when the data is stored as list. When the unwind operator is applied on a list data field, it will generate a new record for each and every element of the list data field on which unwind is applied. It basically flattens the data. Lets see an example to understand this better

Note: The field name, on which unwind is applied, should be prefixed with $ (dollar sign)

Example:Lets apply unwind over "Kalki"'s data.
db.Student.aggregate ([
   {
      "$match":
      {
         "Student_Name": "Kalki",
      }
   }
])

Storing aggregate values into another collection:-
db.newStudent.insert( db.Student.aggregate ([
  {
     "$match":
     {
        "Class":"2"
     }
  }
  ]) )



No comments:

Post a Comment