我有一个Job包含 job_title、job_location、salary 等的 schema
应用架构将子文档嵌入到工作文档中,它存储该特定工作已收到的所有申请
这是作业架构的样子
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"
}
},
]
当一份工作收到申请时,它工作得很好,但当没有收到申请时,它就无法放松,这就是我什么也得不到的地方
那么我怎样才能给出一些条件,以便这个查询在有或没有任何应用程序的情况下都可以工作
我也在查询申请阶段的申请人数,我也有其他阶段。