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.
The aggregate() Method
For the aggregation in mongodb you should use aggregate() method.
SYNTAX:
Basic syntax of aggregate() method is as follows
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
EXAMPLE:
> db.democol.find().pretty()
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"by" : "subramanian",
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
],
"likes" : 20,
"title" : "Learn SQL"
}
{
"_id" : ObjectId("52a62f01fdaa40a1db551d04"),
"title" : "Learn HTML5",
"by" : "KGSM",
"likes" : 240
}
{
"_id" : ObjectId("52a62f3efdaa40a1db551d05"),
"title" : "Learn Java",
"by" : "Faulkner",
"likes" : 209
}
{
"_id" : ObjectId("52a62f61fdaa40a1db551d06"),
"title" : "Basics of Life",
"by" : "Peter",
"likes" : 218
}
{
"_id" : ObjectId("52a65a92fdaa40a1db551d07"),
"title" : "Basics of Life",
"by" : "Peter",
"likes" : 318
}
1.
> db.democol.aggregate([{$group: {_id:"$by",likes:{$sum:"$likes"}}}])
{
"result" : [
{
"_id" : "Peter",
"likes" : 536
},
{
"_id" : "Faulkner",
"likes" : 209
},
{
"_id" : "KGSM",
"likes" : 240
},
{
"_id" : "subramanian",
"likes" : 20
},
{
"_id" : "mani",
"likes" : 100
}
],
"ok" : 1
}
2.
> db.democol.aggregate([{$group: {_id:"$by",likes:{$min:"$likes"}}}])
{
"result" : [
{
"_id" : "Peter",
"likes" : 218
},
{
"_id" : "Faulkner",
"likes" : 209
},
{
"_id" : "KGSM",
"likes" : 240
},
{
"_id" : "subramanian",
"likes" : 20
},
{
"_id" : "mani",
"likes" : 100
}
],
"ok" : 1
}
3.
> db.democol.aggregate([{$group: {_id:"$by",likes:{$max:"$likes"}}}])
{
"result" : [
{
"_id" : "Peter",
"likes" : 318
},
{
"_id" : "Faulkner",
"likes" : 209
},
{
"_id" : "KGSM",
"likes" : 240
},
{
"_id" : "subramanian",
"likes" : 20
},
{
"_id" : "mani",
"likes" : 100
}
],
"ok" : 1
}
Expression | Description | Example |
---|---|---|
$sum | Sums up the defined value from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) |
$avg | Calculates the average of all given values from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) |
$min | Gets the minimum of the corresponding values from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) |
$max | Gets the maximum of the corresponding values from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) |
$push | Inserts the value to an array in the resulting document. | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) |
$addToSet | Inserts 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 | Gets 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 | Gets 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"}}}]) |