MongoDB Aggregation - MongoDB

What is Aggregation in MongoDB?

Aggregations operations in MongoDB, can process data records and return computed results. They can group together, values from multiple documents and can perform various operations on the grouped data to return a single result. In SQL count(*) and with group by is is similar to mongodb aggregation.

aggregate() Method

For the aggregation in MongoDB, you should use aggregate() method.

Syntax

Basic syntax of aggregate() method is as follows

Example

In the collection you have the following data

Now from the above collection, if you wish to display a list showing how many tutorials have been written by each user, use below aggregate() method

Sql corresponding query for the above use case will be select by_user, count(*) from mycol group by by_user.

In the above example, we have grouped documents by field by_user and on each occurrence of by_user, previous value of sum will be incremented. Below is a list of available aggregation expressions.

Expression

Description

Example

$sum

Will sum up the defined value from all documents in the collection.

db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])

$avg

Will calculate the average of all given values from all documents in the collection.

db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])

$min

Will get the minimum of the corresponding values from all documents in the collection.

db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])

$max

Will get the maximum of the corresponding values from all documents in the collection.

db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])

$push

Will insert the value to an array in the resulting document.

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])

$addToSet

Will insert the value to an array in the resulting document but does not create duplicates.

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])

$first

Will get the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.

db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])

$last

Will get the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage.

db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])

Pipeline Concept

In UNIX command, shell pipeline means the possibility of executing an operation on some input and use the output as input for the next command and so on. MongoDB will also support same concept in aggregation framework. There is a set of possible stages and each of these will be taken as a set of documents as an input and will produce a resulting set of documents (or the final resulting JSON document at the end of the pipeline). This in turn can be used for the next stage and so on.

Below are the possible stages in aggregation framework

  • $project− Used for selecting some specific fields from a collection.
  • $match − $match is a filtering operation which reduces the amount of documents which are given as input to the next stage.
  • $group − $group performs the actual aggregation as discussed above.
  • $sort– will sort the documents.
  • $skip− $skip is used to skip forward in the list of documents for a given amount of documents.
  • $limit− $limit will limit the amount of documents to look at, by the given number starting from the current positions.
  • $unwind− $unwind is used to unwind documents which use arrays. While using an array, data will be pre-joined and this operation will be undone with this toget the individual documents again. With this stage amount of documents can be increased for the next stage.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MongoDB Topics