Mongodb 在$unwind之前检查子文档是否为空

Mongodb 在$unwind之前检查子文档是否为空,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一个工作模式,其中包含职位、工作地点、工资等 ApplicationSchema将子文档嵌入到作业文档中,并存储此特定作业收到的所有应用程序 以下是作业模式的外观 const jobSchema = new Schema({ job_title : { type : String, required : true }, job_location : { type : String, }, salary : { type : Number

我有一个工作模式,其中包含职位、工作地点、工资等

ApplicationSchema将子文档嵌入到作业文档中,并存储此特定作业收到的所有应用程序

以下是作业模式的外观

const jobSchema = new Schema({
  job_title : {
    type : String,
    required : true
  },
  job_location : {
    type : String,
  },
  salary : {
    type : Number
  },
  applications: [ApplicationSchema],
  companyId : {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Company'
  }
},{timestamps : true});

var Job = mongoose.model('Job', jobSchema);

module.exports = Job;
您可以看到上面的应用程序子文档

下面是我的工作文档在没有特定工作申请时的外观

 {
"_id" : ObjectId("5ac873c3bb7a9c3168ff159e"),
    "applications" : [],
    "job_title" : "Junior Developer",
    "companyId" : ObjectId("5ac870d0bb7a9c3168ff159c"),
    "createdAt" : ISODate("2018-04-07T07:31:15.257Z"),
    "updatedAt" : ISODate("2018-04-07T09:20:52.237Z"),
    "__v" : 2,
    "job_location" : "Pune, Maharashtra, India",
    "salary" : 3
}
和应用程序

   {
    "_id" : ObjectId("5ac873c3bb7a9c3168ff159e"),
        "applications" : [ 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "rejectedComment" : ""
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "rejectedComment" : ""
            }
        ],
        "job_title" : "Junior Developer",
        "companyId" : ObjectId("5ac870d0bb7a9c3168ff159c"),
        "createdAt" : ISODate("2018-04-07T07:31:15.257Z"),
        "updatedAt" : ISODate("2018-04-07T09:20:52.237Z"),
        "__v" : 2,
        "job_location" : "Pune, Maharashtra, India",
        "salary" : 3
    }
这里是查询

var jobsQuery = [
      {
        $match: {
          companyId: mongoose.Types.ObjectId(req.body.companyId),
          active: req.body.active
        }
      },
      {
        $unwind: "$applications"
      },
      {
        $match : query
      },
      {
        "$group":
                {
                  "_id": "$job_title",
                  "job_title": {$first: "$_id"},
                  "job_location": {$first: "$job_location"},
                  "min_experience": {$first: "$min_experience"},
                  "max_experience": {$first: "$max_experience"},
                  "min_salary": {$first: "$min_salary"},
                  "max_salary": {$first: "$max_salary"},
                  "createdAt": {$first: "$createdAt"},
                  "userId": {$first: "$userId"},

                  "applied": {"$sum":
                                   {"$cond":
                                           [{"$and": [
                                             {"$eq":["$applications.applied", true]},
                                             {"$eq":["$applications.shortlisted", false]},
                                             {"$eq":["$applications.interviewed", false]},
                                             {"$eq":["$applications.offered", false]},
                                             {"$eq":["$applications.hired", false]},
                                             {"$eq":["$applications.rejected", false]},
                                           ]},
                                           1,0]
                                   }
                             },

                }
      },
      {
          "$lookup":
              {
                  from: "users",
                  localField: "userId",
                  foreignField: "_id",
                  as: "userDetail"
              }
      },
    ]
当一份工作收到申请时,它工作得非常好,但当它没有收到申请时,它就不能放松,而这就是我一无所获的原因

那么,我怎样才能给出一些条件,使这个查询在有或没有任何应用程序的情况下都能工作呢

   {
    "_id" : ObjectId("5ac873c3bb7a9c3168ff159e"),
        "applications" : [ 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "rejectedComment" : ""
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "rejectedComment" : ""
            }
        ],
        "job_title" : "Junior Developer",
        "companyId" : ObjectId("5ac870d0bb7a9c3168ff159c"),
        "createdAt" : ISODate("2018-04-07T07:31:15.257Z"),
        "updatedAt" : ISODate("2018-04-07T09:20:52.237Z"),
        "__v" : 2,
        "job_location" : "Pune, Maharashtra, India",
        "salary" : 3
    }

我还在申请阶段查询计数申请者,我还有其他阶段。

您需要在
$unwind
操作中添加
preserveNullAndEmptyArrays
属性,如下所示:

{
 $unwind:
  {
    path: "$applications",
    preserveNullAndEmptyArrays: true
  }
}
如果为true,则如果路径为null、缺少或为空数组,$unwind将输出文档。如果为false,则如果路径为null、缺少或为空数组,则$unwind不会输出文档


在$match阶段,您需要$exists和$ne操作符

你的$match阶段看起来像

{
 $match: {
  companyId: mongoose.Types.ObjectId(req.body.companyId),
  active: req.body.active,
  applications:{$exists:true,$ne:[]}
 }
}
您的整个管道将如下所示

 [
      {
        $match: {
          companyId: mongoose.Types.ObjectId(req.body.companyId),
          active: req.body.active,
          applications:{$exists:true,$ne:[]}
        }
      },
      {
        $unwind: "$applications"
      },
      {
        $match : query
      },
      {
        "$group":
                {
                  "_id": "$job_title",
                  "job_title": {$first: "$_id"},
                  "job_location": {$first: "$job_location"},
                  "min_experience": {$first: "$min_experience"},
                  "max_experience": {$first: "$max_experience"},
                  "min_salary": {$first: "$min_salary"},
                  "max_salary": {$first: "$max_salary"},
                  "createdAt": {$first: "$createdAt"},
                  "userId": {$first: "$userId"},

                  "applied": {"$sum":
                                   {"$cond":
                                           [{"$and": [
                                             {"$eq":["$applications.applied", true]},
                                             {"$eq":["$applications.shortlisted", false]},
                                             {"$eq":["$applications.interviewed", false]},
                                             {"$eq":["$applications.offered", false]},
                                             {"$eq":["$applications.hired", false]},
                                             {"$eq":["$applications.rejected", false]},
                                           ]},
                                           1,0]
                                   }
                             },

                }
      },
      {
          "$lookup":
              {
                  from: "users",
                  localField: "userId",
                  foreignField: "_id",
                  as: "userDetail"
              }
      },
    ] 
更多

您好,如果找不到大于0的应用程序数组,它将立即停止。即使在发现应用程序数组为0之后,它也不应该停止。谢谢你帮我解决了完全相同的问题。