I am trying to group my data day-wise,weekly,monthly and yearly in single pipeline. I have date, week number, month and year in separate fields . But i am not able to figure out how to do group aggregation in a single flow. Below is sample json:
{
"Ctrans" : {
"status" : "active"
},
"Day-month" : "25-9",
"Month" : 9,
"Year" : 2019,
"Week" : 38
}
My query for this pipeline:
db.getCollection("transaction").aggregate(
[
{
"$lookup" : {
"from" : "customer",
"localField" : "pB.phone",
"foreignField" : "phone",
"as" : "Ctrans"
}
},
{
"$unwind" : {
"path" : "$Ctrans",
"includeArrayIndex" : "arrayIndex",
"preserveNullAndEmptyArrays" : false
}
},
{
"$project" : {
"date" : {
"$dateFromString" : {
"dateString" : "$createdAt",
"onError" : "$date"
}
},
"Weekdate" : {
"$dateFromString" : {
"dateString" : "$createdAt",
"onError" : "$date"
}
},
"Ctrans.status" : 1.0,
"createdAt" : 1.0
}
},
{
"$project" : {
"date" : {
"$dateToParts" : {
"date" : "$date"
}
},
"week" : {
"$week" : "$Weekdate"
},
"Ctrans.status" : 1.0
}
},
{
"$project" : {
"Day-month" : {
"$concat" : [
{
"$toString" : "$date.day"
},
"-",
{
"$toString" : "$date.month"
}
]
},
"Month" : "$date.month",
"Year" : "$date.year",
"Week" : "$week",
"Ctrans.status" : 1.0
}
},
{
"$group" : {
"_id" : {
"Today" : "$Day-month",
"Status" : "$status"
},
"Total" : {
"$sum" : 1.0
}
}
}
],
{
"allowDiskUse" : false
}
);
I aim to groupby with pairs of : (Day-month, status),(month,status),(year,status),(week,status) and get count of each paired group separately.