【问题标题】:combing multiple csv rows onto one based on a field基于字段将多个 csv 行合并为一个
【发布时间】: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


    【解决方案1】:

    您快到了,您只需要更改 $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})
    });
    

    【讨论】:

    • 完美运行。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多