Skip to content

Instantly share code, notes, and snippets.

@Abhinay-g
Created May 27, 2019 18:43
Show Gist options
  • Select an option

  • Save Abhinay-g/85e4ae9d3b81562e4de289cc2fe5ecb7 to your computer and use it in GitHub Desktop.

Select an option

Save Abhinay-g/85e4ae9d3b81562e4de289cc2fe5ecb7 to your computer and use it in GitHub Desktop.
MongoD Aggregation framework
Introduction:
Aggregation framework is just an alternative for a find method, here there is more controle given to find data in custom format by using
$match, $sort, $group, $project
This is type of data is only gathered for smaller audiance as we design database based on larger audiance
In aggregation we create a pipeline where in data of one operator is passed to other operator
Aggregation method:
for aggregation we use Aggrigate() method which takes multiple parameter in sequence. Note this aggregation method can take advantage of
mongodb indexes.
Note: for this tutorial persons.json is used
example:
db.persons.aggregate([{ $match:{gender:"female"} }]).pretty()
$match is similar to find method
Method $group :
this method also takes a document for grouping
group method takes two parameter
1) _id : {colname:{key}}
2) aggregated value and method
db.persons.aggregate([
{$match:{gender:"female"}},
{$group:{_id:{state:"$location.state"},totalPersons:{$sum:1}}}
])
$ is used to refer incomming documents
{ "_id" : { "state" : "berkshire" }, "totalPersons" : 1 }
{ "_id" : { "state" : "indre-et-loire" }, "totalPersons" : 1 }
{ "_id" : { "state" : "loiret" }, "totalPersons" : 1 }
{ "_id" : { "state" : "cornwall" }, "totalPersons" : 2 }
Method $sort:
This method also takes document to sort input
example:
db.persons.aggregate([
{$match:{gender:"female"}},
{$group:{_id:{state:"$location.state"},totalPersons:{$sum:1}}},
{$sort:{totalPersons:-1}}])
sons:{$sum:1}}}, {$sort:{totalPersons:-1}}])
{ "_id" : { "state" : "midtjylland" }, "totalPersons" : 33 }
{ "_id" : { "state" : "nordjylland" }, "totalPersons" : 27 }
{ "_id" : { "state" : "australian capital territory" }, "totalPersons" : 24 }
{ "_id" : { "state" : "syddanmark" }, "totalPersons" : 24 }
{ "_id" : { "state" : "new south wales" }, "totalPersons" : 24 }
{ "_id" : { "state" : "south australia" }, "totalPersons" : 22 }
{ "_id" : { "state" : "hovedstaden" }, "totalPersons" : 21 }
*Note : when data will will come to $sort, it can not refer old document, it can only access immediate data propogated
Method $project :
It also takes a document to filter some fields but amazingly it will allow to form some custome fields
db.persons.aggregate([
{$project:{_id:0,gender:1,fullName:{$concat:["$name.first", " ", "$name.last"]}}}
])
{ "gender" : "male", "fullName" : "zachary lo" }
{ "gender" : "male", "fullName" : "victor pedersen" }
{ "gender" : "male", "fullName" : "carl jacobs" }
{ "gender" : "male", "fullName" : "harvey chambers" }
db.persons.aggregate([
{$project:{_id:0,gender:1,fullName:{$concat:[{$toUpper:"$name.first"}, " ",{$toUpper:"$name.first"}]}}}
])
{ "gender" : "male", "fullName" : "ZACHARY ZACHARY" }
{ "gender" : "male", "fullName" : "VICTOR VICTOR" }
{ "gender" : "male", "fullName" : "CARL CARL" }
{ "gender" : "male", "fullName" : "HARVEY HARVEY" }
{ "gender" : "male", "fullName" : "GIDEON GIDEON" }
{ "gender" : "female", "fullName" : "MAEVA MAEVA" }
*Note : $concat takes array of wither documents or string then add them into one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment