Javascript 基于字段将多个csv行合并到一个csv行
我的csv数据如下所示Javascript 基于字段将多个csv行合并到一个csv行,javascript,node.js,mongodb,mongodb-query,aggregation-framework,Javascript,Node.js,Mongodb,Mongodb Query,Aggregation Framework,我的csv数据如下所示 +----------+--+--+--+--+--------------------------+---------+-------+ | Username | | | | | SchoolName | Type | Count | +----------+--+--+--+--+--------------------------+---------+-------+ | corinne | | | | | Bre
+----------+--+--+--+--+--------------------------+---------+-------+
| 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 |
+----------+--+--+--+--+--------------------------+---------+-------+
报告显示每个特定用户对所述学校标记的视频的看法、喜好和评论,是否可以更改报告,以便根据类型将特定学校的每个用户的不同计数显示为3个不同的列?
像
数据是从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})
}
您就快到了,只需使用操作符更改管道以适应计数,它将根据
$type
字段向提供一个计数值
例如:
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": {
"username": "$users.username",
"schoolName": "$schools.name"
},
"viewCount": {
"$sum": {
"$cond": [ { "$eq": [ "$type", "View" ] }, 1, 0 ]
}
},
"likeCount": {
"$sum": {
"$cond": [ { "$eq": [ "$type", "Like" ] }, 1, 0 ]
}
},
"commentCount": {
"$sum": {
"$cond": [ { "$eq": [ "$type", "Comment" ] }, 1, 0 ]
}
}
}
},
{
"$project": {
"Username": "$_id.username",
"SchoolName": "$_id.schoolName",
"_id": 0, "viewCount": 1, "likeCount": 1, "commentCount": 1
}
}
], function(err, totalStats){
console.log(totalStats);
var result=json2csv({data:totalStats})
});
工作完美。非常感谢:)
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": {
"username": "$users.username",
"schoolName": "$schools.name"
},
"viewCount": {
"$sum": {
"$cond": [ { "$eq": [ "$type", "View" ] }, 1, 0 ]
}
},
"likeCount": {
"$sum": {
"$cond": [ { "$eq": [ "$type", "Like" ] }, 1, 0 ]
}
},
"commentCount": {
"$sum": {
"$cond": [ { "$eq": [ "$type", "Comment" ] }, 1, 0 ]
}
}
}
},
{
"$project": {
"Username": "$_id.username",
"SchoolName": "$_id.schoolName",
"_id": 0, "viewCount": 1, "likeCount": 1, "commentCount": 1
}
}
], function(err, totalStats){
console.log(totalStats);
var result=json2csv({data:totalStats})
});