在 $unwind 检查子文档是否不为空之前 [英] Before $unwind check if sub document is not empty
问题描述
我有一个 Job 架构,其中包含 job_title、job_location、salary 等
ApplicationSchema 是嵌入到 Job 文档中的子文档,它存储了该特定作业收到的所有应用程序
这是 Job Schema 的外观
const jobSchema = new Schema({职称 : {类型:字符串,必需:真},工作地点 : {类型:字符串,},薪水 : {类型:数字},应用程序:[ApplicationSchema],公司编号:{类型:mongoose.Schema.Types.ObjectId,参考:公司"}},{时间戳:真});var Job = mongoose.model('Job', jobSchema);module.exports = 工作;
你可以看到上面的应用程序子文档.
这是当没有特定工作申请时我的工作文档的外观
<代码> {"_id" : ObjectId("5ac873c3bb7a9c3168ff159e"),应用程序":[],"job_title" : "初级开发人员","companyId" : ObjectId("5ac870d0bb7a9c3168ff159c"),"createdAt" : ISODate("2018-04-07T07:31:15.257Z"),"updatedAt" : ISODate("2018-04-07T09:20:52.237Z"),__v":2,"job_location" : "印度马哈拉施特拉邦浦那",薪水":3}
还有应用程序
<代码> {"_id" : ObjectId("5ac873c3bb7a9c3168ff159e"),应用程序":[{应用":是的,入围":假,采访":假,提供":假,雇用":假,拒绝":错误,拒绝评论":"},{应用":是的,入围":假,采访":假,提供":假,雇用":假,拒绝":错误,拒绝评论":"}],"job_title" : "初级开发人员","companyId" : ObjectId("5ac870d0bb7a9c3168ff159c"),"createdAt" : ISODate("2018-04-07T07:31:15.257Z"),"updatedAt" : ISODate("2018-04-07T09:20:52.237Z"),__v":2,"job_location" : "印度马哈拉施特拉邦浦那",薪水":3}
这里是查询
var jobsQuery = [{$匹配:{companyId: mongoose.Types.ObjectId(req.body.companyId),活跃:req.body.active}},{$unwind: "$applications"},{$match : 查询},{$组":{"_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"},应用":{$总和":{"$cond":[{$和":[{"$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]}},}},{$查找":{来自:用户",本地字段:用户 ID",外国字段:_id",如:用户详细信息"}},]
当一份工作收到申请时,它工作得非常好,但当它没有时,它就无法 $unwind,这就是我一无所获的地方
那么我怎样才能给出一些条件,以便这个查询在有和没有任何应用程序的情况下都有效
我也在查询申请阶段的申请人数,我也有其他阶段.
您需要在 $unwind
操作中添加 preserveNullAndEmptyArrays
属性,如下所示:
<代码>{$放松:{路径:$应用程序",保留NullAndEmptyArrays:真}}
<块引用>
如果为真,如果路径为空、缺失或为空数组,则 $unwind 输出文档.如果为 false,如果路径为 null、缺失或为空数组,$unwind 不会输出文档.
I have a Job schema that has job_title, job_location, salary, etc
ApplicationSchema is embedded sub document into Job document and it stores all the applications that this particular job has received
Here is how Job Schema looks
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
}
And with applications
{
"_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
}
And here is query
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"
}
},
]
When a job has received applications it works perfectly fine but when it has not, It can't $unwind and that's where i am getting nothing
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.
You need to add preserveNullAndEmptyArrays
property inside your $unwind
operation like this:
{
$unwind:
{
path: "$applications",
preserveNullAndEmptyArrays: true
}
}
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 检查子文档是否不为空之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!