0

我有我的CSV数据如下,梳理多个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 

,我贴的代码,如果有帮助。由于

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}) 
        } 

回答

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}) 
}); 
+1

完美。万分感谢 :) – Karthik