我有工作,每个工作都有应聘者。这是一份工作的样本。

  {
     "_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
          // }
        // }
      }
    }

最佳答案

您可以在$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
  }
])

关于mongodb - Mongodb聚合,分组再计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49192050/

10-09 14:52