我目前正在尝试按月对首次出现的事件进行分组,然后跟踪它们。请参阅下面的更多详细信息!
到目前为止,我得到的结果是针对每个用户的,并且想知道是否需要做另一个小组。到目前为止,这是我的查询:
[
{
// Match
{
"$group": {
"_id": "$user",
"First Payment": {
"$first": "$dateCreated"
},
"Payments": {
"$sum": 1
},
"Total Amount": {
"$sum": "$amount"
}
}
},
{
// Sort
}
]
这为我提供了每个用户的以下信息,并且每次后续付款都在第一次付款后一个月(仅使用前几行作为示例):
{
"_id" : "001",
"First Payment" : ISODate("2017-05-11T16:25:03.000+0000"),
"Payments" : 2.0,
"Total Amount" : NumberInt(30)
},
{
"_id" : "002",
"First Payment" : ISODate("2017-05-13T19:02:04.000+0000"),
"Payments" : 1.0,
"Total Amount" : NumberInt(25)
},
{
"_id" : "003",
"First Payment" : ISODate("2017-05-15T16:42:47.000+0000"),
"Payments" : 3.0,
"Total Amount" : NumberInt(75)
},
{
"_id" : "004",
"First Payment" : ISODate("2017-06-20T13:30:03.000+0000"),
"Payments" : 1.0,
"Total Amount" : NumberInt(25)
}
根据上面的结果,我正在尝试获取如下内容:
{
"_id" : "001",
"First Payment Month" : ISODate("2017-05-01T00:00:00.000+0000"),
"Payment Month Number" : NumberInt(1),
"Unique Users" : NumberInt(3),
"Total Amount" : 65.0
},
{
"_id" : "002",
"First Payment Month" : ISODate("2017-05-01T00:00:00.000+0000"),
"Payment Month Number" : NumberInt(2),
"Unique Users" : NumberInt(2),
"Total Amount" : 40.0
},
{
"_id" : "003",
"First Payment Month" : ISODate("2017-05-01T00:00:00.000+0000"),
"Payment Month Number" : NumberInt(3),
"Unique Users" : NumberInt(1),
"Total Amount" : 25.0
},
{
"_id" : "004",
"First Payment Month" : ISODate("2017-06-01T00:00:00.000+0000"),
"Payment Month Number" : NumberInt(1),
"Unique Users" : NumberInt(1),
"Total Amount" : 25.0
},
非常感谢!