[英]Mongo db first groupby count not able to display
[
{ "$match": {
"created":{
"$gte": ISODate("2015-07-19T07:26:49.045Z")
},
"created":{
"$lte": ISODate("2015-07-20T07:37:56.045Z")
}
}},
{ "$group:{
"_id":{
"ln":"$l.ln",
"cid":"$cid"
},
"appCount":{ "$sum": 1 }
}},
{ "$group": {
"_id": { "ln":"$_id.ln" },
"cusappCount": { "$sum": 1 }
}},
{ "$sort":{ "_id.ln":1 } }
]
In above mongo db query I am not able to display the appcount in result.. I am able to display cusappCount. 在上面的mongo db查询中,我无法显示结果中的appcount。我可以显示cusappCount。 Could anyone please help me on this
有人可以帮我吗
The $match
is wrong to start with and does not do what you think. $match
开头是错误的,不会按照您的想法做。 It is only selecting the "second" statement: 它只是选择“第二”语句:
"created":{
"$lte": ISODate("2015-07-20T07:37:56.045Z")
}
So your selections are incorrect to start with. 因此,您的选择一开始是不正确的。
That and other corrections below: 以及以下的其他更正:
[
{ "$match": {
"created": {
"$gte": ISODate("2015-07-19T07:26:49.045Z"),
"$lte": ISODate("2015-07-20T07:37:56.045Z")
}
}},
{ "$group":{
"_id": {
"ln":"$l.ln",
"cid":"$cid"
},
"appCount":{ "$sum": 1 }
}},
{ "$group": {
"_id": "$_id.ln",
"cusappCount": { "$sum": "$appCount" },
"distinctCustCount": { "$sum": 1 }
}},
{ "$sort":{ "_id": 1 } }
]
Which seems to be what you are trying to do. 这似乎是您要尝试执行的操作。
So your earier "count" is then passed to $sum
when grouping at a "broader" level. 因此,在“更广泛”的级别进行分组时,您较早的“计数”便会传递到
$sum
。 The "second" count is just for the "distinct" items in the earlier key. “第二”计数仅用于较早键中的“不同”项目。
If you are trying to "retain" the values of "appCount", then the problem here is that your "grouping" is "taking away" the detail level that appears at. 如果您试图“保留”“ appCount”的值,那么这里的问题是您的“分组”正在“占用”显示的详细级别。 So for what it is woth, then this is where you use "arrays" in an output structure:
因此,对于这有什么用,那么您可以在输出结构中使用“数组”:
[
{ "$match": {
"created": {
"$gte": ISODate("2015-07-19T07:26:49.045Z"),
"$lte": ISODate("2015-07-20T07:37:56.045Z")
}
}},
{ "$group":{
"_id": {
"ln":"$l.ln",
"cid":"$cid"
},
"appCount":{ "$sum": 1 }
}},
{ "$group": {
"_id": "$_id.ln",
"cusappCount": { "$sum": 1 },
"custs": { "$push": {
"cid": "$_id.cid", "appCount": "$appCount"
}}
}},
{ "$sort":{ "_id": 1 } }
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.