我有作业架构,其中包含job_title,job_location,salary等
ApplicationSchema 是将子文档嵌入到作业文档中,它存储了此特定作业已收到的所有应用程序
以下是Job 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"
}
},
]
当一份工作收到申请时,它的工作完全正常,但如果没有,它就无法放松,而且我什么都没得到
那么我怎么能给出一些条件,这样这个查询可以使用和不使用任何应用程序
我也在询问申请阶段的申请人,我也有其他阶段。
答案 0 :(得分:1)
您需要在preserveNullAndEmptyArrays
操作中添加$unwind
属性,如下所示:
{
$unwind:
{
path: "$applications",
preserveNullAndEmptyArrays: true
}
}
如果为true,如果路径为null,缺少或为空数组,$ unwind将输出文档。如果为false,则$ unwind在路径为空,缺失或空数组时不输出文档。
答案 1 :(得分:0)
你的$ 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"
}
},
]
了解更多 https://docs.mongodb.com/manual/reference/operator/query/exists/