在$ unwind之前检查子文档是否为空 [英] Before $unwind check if sub document is not empty

查看:281
本文介绍了在$ unwind之前检查子文档是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Job 模式,其中嵌入了job_title,job_location,salary等

I have a Job schema that has job_title, job_location, salary, etc

ApplicationSchema 文档到作业文档中,并存储该特定作业已收到的所有应用程序

ApplicationSchema is embedded sub document into Job document and it stores all the applications that this particular job has received

这是作业模式的外观

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;

您可以看到上述应用程序的子文档。

And you can see above applications sub document.

这是我的工作文件在没有特定工作申请的情况下的样子

here is how my job document looks when there is no application for a particular 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"
              }
      },
    ]

收到工作后应用程序可以正常运行,但是当它不能运行时,它不能$ unwind,那是我什么都没得到的地方

所以我怎么给一些条件因此该查询可以在有或没有任何申请的情况下使用

So how can i give some condition so this query works with and without any applications

我也在查询处于申请阶段的申请人数,而我也有其他阶段。

i am also querying for counting applicants in applied stage and i have other stages too.

推荐答案

您需要在 $ unwind preserveNullAndEmptyArrays 属性c>这样的操作:

You need to add preserveNullAndEmptyArrays property inside your $unwind operation like this:

{
 $unwind:
  {
    path: "$applications",
    preserveNullAndEmptyArrays: true
  }
}




如果为true,则如果路径为空,缺少或为空数组,则$ unwind输出文档。如果为false,则如果路径为空,缺少或为空数组,则$ unwind不会输出文档。

If true, if the path is null, missing, or an empty array, $unwind outputs the document. If false, $unwind does not output a document if the path is null, missing, or an empty array.

$ unwind

这篇关于在$ unwind之前检查子文档是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆