如何根据日期汇总结果 [英] How to aggregate results based on date
本文介绍了如何根据日期汇总结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是mongo的新手,需要帮助汇总以下数据以匹配条件status = New并计算到那天。
例如:
以下收藏:
I am new to mongo and need help in aggregating the following data for matching condition "status=New" and get count till that day.
For example:
For below collection:
[
{
"_id": "abcd123",
"Date": ISODate("2018-06-08T09:42:36.000Z"),
"status": "New",
},
{
"_id": "abcd124",
"Date": ISODate("2018-06-08T09:42:36.000Z"),
"status": "New"
},
{
"_id": "abcd125",
"Date": ISODate("2018-06-09T09:31:44.000Z"),
"status": "New"
},
{
"_id": "abcd126",
"Date": ISODate("2018-06-10T09:42:43Z"),
"status": "New"
},
]
预期结果:
Expected result:
[
{
"_id": null,
"name": "New",
"series": [
{
"name": ISODate("2018-06-08T09:42:36Z"),
"value": 2
},
{
"name": ISODate("2018-06-09T09:31:44Z"),
"value": 3 ( 2 + 1)--> Previous date + current date
},
{
"name": ISODate("2018-06-10T09:42:43Z"),
"value": 4 (3 + 1) --> previous date sum + current date
}
]
}
]
< b>我尝试过:
What I have tried:
db.collection.aggregate([
{
$match: {
status: "New"
}
},
{
$sort: {
Date: -1
}
},
{
$group: {
_id: "$Date",
value: {
$sum: 1
}
}
},
{
$group: {
_id: null,
series: {
$push: {
name: "$_id",
value: "$value"
}
}
}
},
{
$addFields: {
name: "New"
}
},
])
以上查询结果显示每个日期的计数,但不会增加以前的日期数。
感谢任何帮助导致预期的结果。谢谢。
Above query results the count for each date but doesn't increment previous dates count.
Appreciate any help that can lead to expected result. Thank you.
推荐答案
匹配:{
状态:新
}
},
{
match: { status: "New" } }, {
sort:{
Date:-1
}
},
{
sort: { Date: -1 } }, {
group:{
_id:
group: { _id: "
这篇关于如何根据日期汇总结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文