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

编程入门 行业动态 更新时间:2024-10-25 02:28:58
本文介绍了在$ 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.bodypanyId), 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 这样的操作:

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之前检查子文档是否为空

本文发布于:2023-11-22 18:42:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   文档   unwind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!