What are aggregations?

$match

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

$group

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

$project

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

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

{ _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"
}
}
{"_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")}
{"_id" : null,"total" : 61.0,"average_transaction_amount" : 20.333333333333332,"min_transaction_amount" : 12.0,"max_transaction_amount" : 29.0}

ComputerVision | NLP | Kaggle Master. Ex International Financial Analyst. Linkedin — https://bit.ly/3yBFni6 | My Youtube Channel — https://bit.ly/3zGNvzc