Image for post
Image for post

What are aggregations?

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

$match

$match: {  date_written: {     $gte: startDate,     $lt: endDate}

$group

$group: {  _id: "$city",  total_books_written: {     $sum: "$no_of_books_sold"
}
}
Image for post
Image for post
{  _id: "$city",  total_books_written: {    $sum: "$no_of_books_sold"  }}
Image for post
Image for post

$project

$project: {  _id: "$_id",  total_books_written: "$total_books_written",  city: "$_id"}

Few words on the Collection.populate function in the above code

Image for post
Image for post
{ _id: 5cade9a945a3df38a9bb8503,
total_books_written: 42,
city: 5cade9a945a3df38a9bb8503
}
path: 'city'
select: "name"

More exlanation around $project

{
"_id": {
"$oid": "56348e7938b1ab3c382d3363"
},
"carer_id": "55e6f647f081105c299bb45d",
"user_id": "55f000a2878075c416ff9879",
"starting_date": {
"$date": "2015-10-15T05:41:00.000Z"
},
"ending_date": {
"$date": "2015-11-19T10:03:00.000Z"
},
"amount": "850",
"total_days": "25",
}
{
"_id": {
"$oid": "563b5747d6e0a50300a1059a"
},
"carer_id": "55e6f647f081105c299bb45d",
"user_id": "55f000a2878075c416ff9879",
"starting_date": {
"$date": "2015-11-06T04:40:00.000Z"
},
"ending_date": {
"$date": "2015-11-16T04:40:00.000Z"
},
"amount": "25",
"total_days": "10"
}
{
$project: {
myyear: {$year: "$ending_date"}
}
},
{
$match: {
carer_id : req.params.carer_id,
status : 3,
$myyear : "2015"
}
}
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
{"_id" : ObjectId("5caf285d4ea2f23a4914649b"),"productId" : "1","customerId" : "1","amount" : 20.0,"transactionDate" : ISODate("2016-02-23T15:25:56.314+0000")}// ----------------------------------------------{"_id" : ObjectId("5caf28874ea2f23a491464a0"),"productId" : "2","customerId" : "2","amount" : 29.0,"transactionDate" : ISODate("2016-04-23T15:25:56.314+0000")}// ----------------------------------------------{"_id" : ObjectId("5caf28b04ea2f23a491464a5"),"productId" : "3","customerId" : "3","amount" : 85.0,"transactionDate" : ISODate("2019-02-23T15:25:56.314+0000")}// ----------------------------------------------{"_id" : ObjectId("5caf28d44ea2f23a491464a6"),"productId" : "4","customerId" : "4","amount" : 12.0,"transactionDate" : ISODate("2017-06-23T15:25:56.314+0000")}
Image for post
Image for post
{"_id" : null,"total" : 61.0,"average_transaction_amount" : 20.333333333333332,"min_transaction_amount" : 12.0,"max_transaction_amount" : 29.0}
Image for post
Image for post
Image for post
Image for post

Written by

DataScience | ML | 2x Kaggle Expert. Ex Fullstack Engineer and Ex International Financial Analyst. https://www.linkedin.com/in/rohan-paul-b27285129/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store