I am defining a mongodb aggregate expression which will be stored in JSON and executed. The current aggregate expression is mostly simple grouping and projection.
for e.g. below is the aggregate query for grouping in follow up date
[{
"$group": {
"_id": {
"day_of_month": {
"$dayOfMonth": "$data.follow_up_date"
},
"month": {
"$month": "$data.follow_up_date"
},
"year": {
"$year": "$data.follow_up_date"
}
},
"total_leads": {
"$sum": 1
},
"total_loan_amount": {
"$sum": "$data.loan_amount"
},
"follow_up_date": {
"$push": "$data.follow_up_date"
},
"data": {
"$push": "$data"
}
}
},
{
"$project": {
"_id": 1,
"day_of_month": "$_id.day_of_month",
"month": "$_id.month",
"year": "$_id.year",
"total_leads": "$total_leads",
"total_loan_amount": "$total_loan_amount",
"follow_up_date": {
"$arrayElemAt": ["$follow_up_date", 0]
},
"data": "$data"
}
}]
In this aggregate expression I would like to add dynamic date based filter which would allow me to fetch date only say for current month or current day of month.
for e.g.
[{
"$group": {
"_id": {
"day_of_month": {
"$dayOfMonth": "$data.follow_up_date"
},
"month": {
"$month": "$data.follow_up_date"
},
"year": {
"$year": "$data.follow_up_date"
}
},
"total_leads": {
"$sum": 1
},
"total_loan_amount": {
"$sum": "$data.loan_amount"
},
"follow_up_date": {
"$push": "$data.follow_up_date"
},
"data": {
"$push": "$data"
}
}
},
{
//something like this
"_id.day_of_month" : {$month : new Date()}
},
{
"$project": {
"_id": 1,
"day_of_month": "$_id.day_of_month",
"month": "$_id.month",
"year": "$_id.year",
"total_leads": "$total_leads",
"total_loan_amount": "$total_loan_amount",
"follow_up_date": {
"$arrayElemAt": ["$follow_up_date", 0]
},
"data": "$data"
}
}]
Is there any way to add dynamic date filters ?