我的MongoDB查询输出如下:
db.campaign.find({},{"_id":0,"Name":1, "Info.subscriber_id":1})
的产率:
{ "Info" : [ { "subscriber_id" : "" } ], "Name" : "C1" }
{ "Info" : [ { "subscriber_id" : "1" } ], "Name" : "C2" }
{ "Info" : [ { "subscriber_id" : "1" } ], "Name" : "C3" }
{ "Info" : [ { "subscriber_id" : "2" } ], "Name" : "C4" }
我想在MongoDB中找出相同的组中的不同计数:
select subscriber, count(*) totalCount, count(distinct Name) distinctCount
FROM campaign GROUP BY subscriber;
我已在MongoDB查询下构建,以获得与上述sql查询等效的结果集:
db.campaign.aggregate([
{"$group":{
"_id":{
"subscriber": "$Info.subscriber_id",
"Name":"$Name"
},
"count":{"$sum": 1}
}},
{"$group":{
"_id":{
"subscriber": "$_id.subscriber"
},
"totalCount":{"$sum":"$count"},
"distinctCount":{"$sum: 1"}
}}
]);
但是在执行上面的mongodb查询时,我收到的错误如下:
2016-05-20T14:50:47.973 + 0530 SyntaxError:意外的令牌}
如果我只从上面的查询中执行第一组,我将获得结果集。
db.campaign.aggregate([
{"$group":{
"_id":{
"subscriber": "$Info.subscriber_id",
"Name":"$Name"
},
"count":{"$sum": 1}
}}
]);
{ "_id" : { "subscriber" : [ "" ], "Name" : "c1" }, "count" : 1 }
{ "_id" : { "subscriber" : [ "1" ], "Name" : "c1" }, "count" : 1 }
{ "_id" : { "subscriber" : [ "1" ], "Name" : "c3" }, "count" : 1 }
{ "_id" : { "subscriber" : [ "2" ], "Name" : "c2" }, "count" : 1 }
{ "_id" : { "subscriber" : [ "2" ], "Name" : "c4" }, "count" : 1 }
{ "_id" : { "subscriber" : [ "3" ], "Name" : "c3" }, "count" : 1 }
有人可以让我知道我的查询有什么问题。我使用的是MongoDB2.6。