MongoDB 组中的多个累加器日、周、月和年的聚合查询
Multiple accumulators in MongoDB Group Aggregation query for day,week,month and year
我正在尝试将我的数据按天、每周、每月和每年分组在单个 pilepline 中。我在不同的字段中有日期、周数、月份和年份。但我无法弄清楚如何在单个流程中进行组聚合。
下面是示例 json :
{
"Ctrans" : {
"status" : "active"
},
"Day-month" : "25-9",
"Month" : 9,
"Year" : 2019,
"Week" : 38
}
我对该管道的查询:
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
}
);
我的目标是使用成对的 groupby:(Day-month, status),(month,status),(year,status),(week,status)
并分别获得每个配对组的 count。
您可以使用 $facet 在您的数据集上应用单独的 $group
阶段,尝试:
db.collection.aggregate([
// current aggregation stages,
{
$facet: {
"dayMonthStatus": [
{ $group: { _id: { status: "$Ctrans.status", "dayMonth": "$Day-month" }, count: { $sum: 1 } } }
],
"monthStatus": [
{ $group: { _id: { status: "$Ctrans.status", "month": "$Month" }, count: { $sum: 1 } } }
],
"yearStatus": [
{ $group: { _id: { status: "$Ctrans.status", "year": "$Year" }, count: { $sum: 1 } } }
],
"weekStatus": [
{ $group: { _id: { status: "$Ctrans.status", "week": "$Week" }, count: { $sum: 1 } } }
]
}
}
])
我正在尝试将我的数据按天、每周、每月和每年分组在单个 pilepline 中。我在不同的字段中有日期、周数、月份和年份。但我无法弄清楚如何在单个流程中进行组聚合。 下面是示例 json :
{
"Ctrans" : {
"status" : "active"
},
"Day-month" : "25-9",
"Month" : 9,
"Year" : 2019,
"Week" : 38
}
我对该管道的查询:
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
}
);
我的目标是使用成对的 groupby:(Day-month, status),(month,status),(year,status),(week,status)
并分别获得每个配对组的 count。
您可以使用 $facet 在您的数据集上应用单独的 $group
阶段,尝试:
db.collection.aggregate([
// current aggregation stages,
{
$facet: {
"dayMonthStatus": [
{ $group: { _id: { status: "$Ctrans.status", "dayMonth": "$Day-month" }, count: { $sum: 1 } } }
],
"monthStatus": [
{ $group: { _id: { status: "$Ctrans.status", "month": "$Month" }, count: { $sum: 1 } } }
],
"yearStatus": [
{ $group: { _id: { status: "$Ctrans.status", "year": "$Year" }, count: { $sum: 1 } } }
],
"weekStatus": [
{ $group: { _id: { status: "$Ctrans.status", "week": "$Week" }, count: { $sum: 1 } } }
]
}
}
])