Mongodb聚合,分组再计数

时间:2018-03-09 10:49:34

标签: mongodb mongodb-query aggregation-framework

我有工作,候选人已经申请了每份工作。这是一份工作的样本。

  {
     "_id": ObjectId("5a9e625c748a563a80e5d86a"),
     "job_title": "Software developer",
     "companyId": ObjectId("5a9e625c748a563a80e5889ds")
     "applications" : [ 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false
            },
            {
                "applied" : true,
                "shortlisted" : true,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false
            },
            {
                "applied" : true,
                "shortlisted" : true,
                "interviewed" : true,
                "offered" : false,
                "hired" : false,
                "rejected" : false
            },
            {
                "applied" : true,
                "shortlisted" : true,
                "interviewed" : true,
                "offered" : false,
                "hired" : false,
                "rejected" : false
            },
            {
                "applied" : true,
                "shortlisted" : true,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : true
            },

        ]

    }

候选人可以通过应用,入围,接受采访......

等阶段

所以我想得的是每个阶段的申请人总数。

结果如下:

{
  applied: 1,
  shortlisted: 1,
  interviewed: 2,
  offered: 0,
  hired: 0,
  rejected: 1
}

以下是我尝试编写查询的方法

  Job.aggregate([
    {
      $match: {
        companyId : mongoose.Types.ObjectId(req.params.id),
        active: true
      }
    },
    {
        $unwind: "$applications"
    },
    {
      $replaceRoot : {
        newRoot : "$applications"
      }
    },
    {
      $group: {
          _id : {
            applied : "$applied",
            shortlisted : "$shortlisted",
            interviewed : "$interviewed",
            offered : "$offered",
            hired : "$hired",
            rejected : "$rejected"
          },
          applied: {$sum: "_id"},
          // shortlisted: {$sum: "shortlisted"},
          // interviewed: {$sum: "interviewed"},
          // offered: {$sum: "offered"},
          // hired: {$sum: "hired"},
          // rejected: {$sum: "rejected"},
      }
    }
  ])

无论工作如何,这都是总计数。我也想知道每个阶段的申请人数量与工作有关。

已编辑:

{
            "applied" : true,
            "shortlisted" : false,
            "interviewed" : false,
            "offered" : false,
            "hired" : false,
            "rejected" : false,
            "rejectedComment" : "",
            "notes" : [],
            "documents" : [],
            "assessment" : [],
            "interviewSchedule" : [],
            "references" : [],
            "_id" : ObjectId("5a9fc2b6e80e10405ce612d6"),
            "talentId" : ObjectId("5a8e7da63925a10b68ea08dd"),
            "createdAt" : ISODate("2018-03-07T10:45:10.675Z"),
            "updatedAt" : ISODate("2018-03-07T10:45:10.675Z")
        }

已更新

{
      $match: {
        companyId : mongoose.Types.ObjectId(req.params.id),
        active: true
      }
    },
    {
        $unwind: "$applications"
    },
    {
      $replaceRoot : {
        newRoot : "$applications"
      }
    },
    {
      $group: {
          _id : '',
          // "applied":{"$sum":{"$cond":{$and:[{$eq:["$applications", {applied: true, shortlisted: false , interviewed: false, offered: false, hired: false, rejected: false}]},1,0]}}},
          "applied": {"$sum": {"$cond": {$and: [{$eq:["$applications", {applied: true, shortlisted: false , interviewed: false, offered: false, hired: false, rejected: false}]},1,0]}]}}
          //   shortlisted : {"$applied.shortlisted" : false},
          //   interviewed : {"$applied.interviewed" : false},
          //   offered : {"$applied.offered" : false},
          //   hired : {"$applied.hired" : false},
          // "rejected":{"$sum":{"$cond":[{$eq:["$applications", {rejected: true}]},1,0]}},
          // },
          // "applied": {
          //   "$sum": 1
          // }
        // }
      }
    }

2 个答案:

答案 0 :(得分:0)

您可以在$cond内尝试使用$sum运算符来计算每个阶段的应用程序。

这样的东西
Jobs.aggegate([
  {"$match":{"companyId":mongoose.Types.ObjectId(req.params.id),"active":true}},
  {"$unwind":"$applications"},
  {"$group":{
    "_id": some group key,
    "applied":{"$sum":{"$cond":["$applied",1,0]}}
    ...other stages
  }
])

答案 1 :(得分:0)

您可以$unwind使用$group$reduce $cond

使用$sum
db.jobs.aggregate([
    //{$match : {}},
    {$project : {
        data : {
            $reduce : {
                input : "$applications",
                initialValue : {
                    "applied" : 0,
                    "shortlisted" : 0,
                    "interviewed" : 0,
                    "offered" : 0,
                    "hired" : 0,
                    "rejected" : 0
                },
                in : {
                    "applied" : {$sum : ["$$value.applied", {$cond : ["$$this.applied", 1, 0]}]},
                    "shortlisted" : {$sum : ["$$value.shortlisted", {$cond : ["$$this.shortlisted", 1, 0]}]},
                    "interviewed" : {$sum : ["$$value.interviewed", {$cond : ["$$this.interviewed", 1, 0]}]},
                    "offered" : {$sum : ["$$value.offered", {$cond : ["$$this.offered", 1, 0]}]},
                    "hired" : {$sum : ["$$value.hired", {$cond : ["$$this.hired", 1, 0]}]},
                    "rejected" : {$sum : ["$$value.rejected", {$cond : ["$$this.rejected", 1, 0]}]}
                }
            }
        }
    }},
    {$replaceRoot : {newRoot : "$data"}}
]).pretty()

结果

{
    "applied" : 5,
    "shortlisted" : 4,
    "interviewed" : 2,
    "offered" : 0,
    "hired" : 0,
    "rejected" : 1
}
>