我正在尝试查询按日期对输出进行分组的查询,但是下一个字段将基于不同的日期字段。
因此,对于2018-11(年-月)日期,有多少寄存器,有多少激活,有多少客户以及有多少取消。但是每个注册/激活/客户/取消,都必须在发生的月份计算。
我的数据存储如下:
{
"track" : {
"hasBeenCustomer" : true,
"hasActivated" : true,
"hasActivatedAt" : ISODate("2018-08-21T14:32:53.929Z"),
"hasBeenCustomerAt" : ISODate("2019-02-26T07:21:06Z"),
"hasRegisteredAt" : ISODate("2018-08-09T10:17:38.329Z"),
"hasCanceled" : true,
"hasCanceledAt" : ISODate("2019-04-29T13:56:04Z")
}
}
{
"track" : {
"hasBeenCustomer" : true,
"hasActivated" : true,
"hasActivatedAt" : ISODate("2018-08-26T15:04:58.854Z"),
"hasBeenCustomerAt" : ISODate("2018-11-24T10:37:14Z"),
"hasRegisteredAt" : ISODate("2018-08-25T11:12:36.309Z"),
"hasCanceled" : true,
"hasCanceledAt" : ISODate("2019-05-30T18:11:04Z")
}
}
{
"track" : {
"hasBeenCustomer" : true,
"hasActivated" : true,
"hasActivatedAt" : ISODate("2018-09-24T23:21:55.733Z"),
"hasBeenCustomerAt" : ISODate("2019-03-12T10:26:01Z"),
"hasRegisteredAt" : ISODate("2018-09-22T17:56:57.256Z"),
"hasCanceled" : true,
"hasCanceledAt" : ISODate("2019-04-12T10:22:03Z")
}
}
{
"track" : {
"hasBeenCustomer" : true,
"hasActivated" : true,
"hasActivatedAt" : ISODate("2018-10-18T15:08:15.351Z"),
"hasBeenCustomerAt" : ISODate("2018-12-22T21:37:01Z"),
"hasRegisteredAt" : ISODate("2018-10-16T03:54:16.056Z"),
"hasCanceled" : true,
"hasCanceledAt" : ISODate("2019-01-22T21:39:03Z")
}
}
我已经尝试过了:
db.user.aggregate(
[
{
$match:
{
projectId : "00001"
}
},
{
"$project": {
"createDate": {
"$dateToString": {
"format": "%Y-%m",
"date": "$track.hasRegisteredAt"
}
},
activationAt: {
"$dateToString": {
"format": "%Y-%m",
"date": "$track.hasActivatedAt"
}
},
customerAt: {
"$dateToString": {
"format": "%Y-%m",
"date": "$track.hasBeenCustomerAt"
}
},
cancelAt: {
"$dateToString": {
"format": "%Y-%m",
"date": "$track.hasCanceledAt"
}
},
activations: {
"$sum": {
"$cond": [
{ "$eq": [ "$track.hasActivated", true ] },
1,
0
]
}
},
customers: {
"$sum": {
"$cond": [
{ "$eq": [ "$track.hasBeenCustomer", true ] },
1,
0
]
}
},
cancels: {
"$sum": {
"$cond": [{
"$and": [
{ "$eq": [ "$status", 3 ] },
{ "$eq": [ "$track.hasCanceled", true ] }
]},
1,
0
]
}
}
}
},
{
$group:
{
_id: "$createDate",
users: {$sum: 1},
activations: {$sum: "$activations"},
activationsM: {
"$sum": {
"$cond": [
{ "$eq": [ "$activationAt", "$createDate" ] },
1,
0
]
}
},
customers: {$sum: "$customers"},
customersM: {
"$sum": {
"$cond": [
{ "$eq": [ "$customerAt", "$createDate" ] },
1,
0
]
}
},
cancels: {$sum: "$cancels"},
cancelsM: {
"$sum": {
"$cond": [
{ "$eq": [ "$cancelAt", "$createDate" ] },
1,
0
]
}
},
}
},
{
$sort:
{
_id: 1
}
}
]
)
activationsM,customersM,cancelsM应该是独立于_id字段的每月计数,但我已经意识到,此查询消除了与_id匹配的结果,一旦匹配,便检查条件。即使_id与hasActivatedAt,hasBeenCustomerAt,hasCanceledAt字段不匹配,我也需要求和。
希望我已经正确解释了。
所需的输出将是:
{ "_id" : "2018-06", "users" : 18, "activations" : 5, "activationsM" : 2, "customers" : 4, "customersM" : 0, "cancels" : 1, "cancelsM" : 0 }
{ "_id" : "2018-07", "users" : 78, "activations" : 39, "activationsM" : 31, "customers" : 11, "customersM" : 0, "cancels" : 7, "cancelsM" : 0 }
{ "_id" : "2018-08", "users" : 115, "activations" : 49, "activationsM" : 38, "customers" : 18, "customersM" : 0, "cancels" : 8, "cancelsM" : 0 }
成为 fiedlM 对应字段日期和_id日期字段的总数。
谢谢。
答案 0 :(得分:1)
请尝试以下操作:
if fetchEvents.count > 0 {
completion(fetchEvents.count)
}
结果如下:
db.collection.aggregate([
{
$facet: {
"TOTAL_ACTIVATION": [
{
$group: {
_id: "$track.hasActivated",
total: { "$sum": 1 },
"totalActiveCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasActivated", true ] },
1,
0
]}
},
"totalNonActiveCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasActivated", false ] },
1,
0
]}
},
}
}
],
"TOTAL_CUSTOMERS": [
{
$group: {
_id: "$track.hasBeenCustomer",
total: { "$sum": 1 },
"totalCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasBeenCustomer", true ] },
1,
0
]}
},
"totalNonCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasBeenCustomer", false ] },
1,
0
]}
},
}
}
],
"TOTAL_CANCELLED": [
{
$group: {
_id: "$track.hasCanceled",
total: { "$sum": 1 },
"totalCancelledCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasCanceled", true ] },
1,
0
]}
},
"totalNonCancelledCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasCanceled", false ] },
1,
0
]}
},
}
}
],
"MONTHLY_ACTIVATION" : [
{
$group: {
_id: {
year: { $year: "$track.hasActivatedAt" },
month: { $month: "$track.hasActivatedAt" }
},
totalThisMonth: { $sum : 1},
"totalActiveCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasActivated", true ] },
1,
0
]}
},
"totalNonActiveCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasActivated", false ] },
1,
0
]}
},
}
}
],
"MONTHLY_CUSTOMER" : [
{
$group: {
_id: {
year: { $year: "$track.hasBeenCustomerAt" },
month: { $month: "$track.hasBeenCustomerAt" }
},
totalThisMonth: { $sum : 1},
"totalCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasBeenCustomer", true ] },
1,
0
]}
},
"totalNonCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasBeenCustomer", false ] },
1,
0
]}
},
}
}
],
"MONTHLY_CANCELLED" : [
{
$group: {
_id: {
year: { $year: "$track.hasCanceledAt" },
month: { $month: "$track.hasCanceledAt" }
},
totalThisMonth: { $sum : 1},
"totalCancelledCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasCanceled", true ] },
1,
0
]}
},
"totalNonCancelledCustomer": {
"$sum": { "$cond": [
{ "$eq": [ "$track.hasCanceled", false ] },
1,
0
]}
},
}
}
]
}
}
])