基于字段将多个csv行组合到一个上

时间:2017-04-05 09:24:22

标签: javascript node.js mongodb mongodb-query aggregation-framework

我的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 |
+----------+--+--+--+--+--------------------------+---------+-------+

该报告显示了每个特定用户对上述学校标记的视频的观看,喜欢和评论,是否可以更改报告,以便将不同的计数显示为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})
                  }

1 个答案:

答案 0 :(得分:0)

您几乎就在那里,您只需要更改 $group 管道以使用 $cond 运算符来调整计数,该运算符将会提供 $sum 一个计数值,具体取决于 $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})
});