【发布时间】:2017-04-05 17:53:09
【问题描述】:
我的 csv 数据如下,
+----------+--+--+--+--+--------------------------+---------+-------+
| Username | | | | | SchoolName | Type | Count |
+----------+--+--+--+--+--------------------------+---------+-------+
| corinne | | | | | Brentwood School | Comment | 1 |
| corinne | | | | | 1st Cerebral Palsy of Nj | Comment | 3 |
| corinne | | | | | Campbell Hall School | Like | 1 |
| ed | | | | | Campbell Hall School | View | 5 |
| ed | | | | | Campbell Hall School | Like | 2
| ed | | | | | 1st Cerebral Palsy of Nj | View | 3 |
| corinne | | | | | 1st Cerebral Palsy of Nj | View | 1 |
| corinne | | | | | 1st Cerebral Palsy of Nj | Like | 1 |
+----------+--+--+--+--+--------------------------+---------+-------+
报告显示每个特定用户对带有上述学校标签的视频的观看次数、喜欢和 cmets,是否可以更改报告,以便将不同的计数显示为 3 个不同的列,对于特定的每个用户学校根据类型? 喜欢,
Username SchoolName viewCount likeCount commentCount
corinne 1st Cerebral Palsy of Nj 1 1 3
ed Campbell Hall School 5 2 0
数据是从 mongo 聚合查询中获得的,如果有帮助,我已经粘贴了代码。谢谢
activity.aggregate([
{ "$match": {createdAt:{$gte:new Date(fromDate), $lte:new Date(toDate)}}},
{ "$unwind": "$category"},
{ "$lookup": {
"localField": "user_id",
"from": "users",
"foreignField": "_id",
"as":"users"
} },
{ "$unwind": "$users" },
{ "$lookup": {
"localField": "category",
"from": "categories",
"foreignField": "_id",
"as":"schools"
} },
{ "$unwind": "$schools" },
{ "$match":matchFilter},
{"$group": {_id:{user:"$users.username", user_id:"$users._id", firstName:"$users.firstName", lastName:"$users.lastName", email:"$users.email",schoolName:"$schools.name",type:"$type"},
"count": { $sum: 1 }}}
], function(err, totalStats){
var finalTotal=[];
async.each(totalStats,function(total,callback){
finalTotal.push({Username:total._id.user, FirstName:total._id.firstName,
LastName:total._id.lastName, Email:total._id.email, UserId:total._id.user_id,SchoolName:total._id.schoolName,Type:total._id.type, Count:total.count})
callback()
},function(err){
if(finalTotal.length>=1){
var result=json2csv({data:finalTotal})
}
【问题讨论】:
标签: javascript node.js mongodb mongodb-query aggregation-framework