我的乔布斯收藏如下
{
"_id" : ObjectId("5b6979339bcdb5d4ae64498f"),
"jobProfile" : "Java Developer",
"jobID" : "1",
"companyName" : "xyz",
"openingsCount" : 10,
"jobType" : "Fulltime",
"jobDescription" : "profile description",
"contactEmail" : "xyz@gmail.com",
"contactWebsite" : "www.xyz.com",
"postingDate" : "07-08-2018 04:21:22",
"jobstatus" : "Active",
"jobEndDate" : ISODate("2018-08-14T23:59:59.999Z"),
"jobEndDateMilliSeconds" : 1534204859999.0
}
{
"_id" : ObjectId("5b6aba4c9bcdb5d4ae649197"),
"jobProfile" : "Python Developer",
"jobID" : "2",
"companyName" : "abc",
"openingsCount" : 10,
"jobType" : "Fulltime",
"jobDescription" : "profile description",
"contactEmail" : "abc@gmail.com",
"contactWebsite" : "www.abc.com",
"postingDate" : "07-08-2018 04:21:22",
"jobstatus" : "Active",
"jobEndDate" : ISODate("2018-08-14T23:59:59.999Z"),
"jobEndDateMilliSeconds" : 1534204859000.0
}
{
"_id" : ObjectId("5b6aba4c9bcdb5d4ae64919q"),
"jobProfile" : "nodejs Developer",
"jobID" : "3",
"companyName" : "abc",
"openingsCount" : 15,
"jobType" : "Fulltime",
"jobDescription" : "profile description",
"contactEmail" : "abc@gmail.com",
"contactWebsite" : "www.abc.com",
"postingDate" : "07-08-2018 04:21:22",
"jobstatus" : "not Active",
"jobEndDate" : ISODate("2018-08-14T23:59:59.999Z"),
"jobEndDateMilliSeconds" : 1534204859000.0
}
我的用户集合是
"userID" : "1",
"userAppliedJobs" : [
"1",
"2",
"3"
]
我希望在具有userID:1的userAppliedJobs数组中存在的所有具有jobID的文档以及jobStatus都处于活动状态。
答案 0 :(得分:0)
您可以使用Mongo 3.6 $lookup
,然后将$projection
与$filter
一起使用:
db.getCollection('jobusers').aggregate([
{ $lookup: {
from: 'jobs',
localField: 'userAppliedJobs',
foreignField: 'jobID',
as: 'userJobs',
}
},
{ "$project": {
"id": 1,
"jobs": {
"$filter": {
"input": "$userJobs",
"as": "job",
"cond": { "$eq": [ "$$job.jobstatus", "Active" ] }
}
}
}}
])
在此示例中,具有3个作业的集合称为jobs
,而具有单个用户jobusers
的集合称为。更改此名称以使其与您的姓名相符。
$lookup部分是必需的,因为您要与另一个集合进行交叉引用。有点棘手的部分是projection filtering。