my Jobs is like below
{
"_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
}
和我的Shortlistjobs集合在下面
{
"_id" : ObjectId("5b6abaaf9bcdb5d4ae6491d2"),
"jobID" : "1",
"jobstatus" : "Active",
"applicantsCount" : 10
}
{
"_id" : ObjectId("5b6ababc9bcdb5d4ae6491f2"),
"jobID" : "2",
"jobstatus" : "Active",
"applicantsCount" : 8
}
{
"_id" : ObjectId("5b6abac59bcdb5d4ae6491f8"),
"jobID" : "3",
"jobstatus" : "Active",
"applicantsCount" : 8
}
我希望所有在Jobs集合中存在的工作都与Shortlistjobs集合中的申请人数一起出现。从Shortlistjobs集合到Jobs集合文档中只能包含一个字段。
我想要如下输出:
{
"_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,
"applicantsCount" : 10
}
{
"_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,,
"applicantsCount" : 8
}
我正在使用以下查询
db.Jobs.aggregate([
{ $match : {jobstatus:"Active"}},
{ $lookup:
{
from : "Shortlistjobs",
localField : "jobID",
foreignField: "jobID",
as : "meta"
}
},
{
$unwind: {
path : "$meta"
}
},
{
$sort: {
'meta.applicantsCount':-1
}
},{$limit:1}
])
以下我从上述查询中得到的输出
{
"_id" : ObjectId("5b6979339bcdb5d4ae64498f"),
"jobProfile" : "Java Developer",
"jobID" : "1",
"companyName" : "IAAA Technologies",
"jobCity" : "Bangalore",
"jobCountry" : "India",
"openingsCount" : 10,
"jobType" : "Fulltime",
"jobDescription" : "profile description",
"contactEmail" : "iaaatech@gmail.com",
"contactWebsite" : "www.iaaatech.com",
"postingDate" : "07-08-2018 04:21:22",
"jobstatus" : "Active",
"jobEndDate" : ISODate("2018-08-14T23:59:59.999Z"),
"jobEndDateMilliSeconds" : 1534204859999.0,
"meta" : {
"_id" : ObjectId("5b6abaaf9bcdb5d4ae6491d2"),
"jobID" : "1",
"jobstatus" : "Active",
"applicantsCount" : 10
}
}
答案 0 :(得分:0)
您可以使用$project从嵌套对象中使用($addFields)和所有其他必填字段来添加特定字段:
db.Jobs.aggregate([
{$lookup:{ from: 'Shortlistjobs ', localField: 'jobID', foreignField: "jobID", as: "meta" }},
{$unwind: "$meta"},
{$addFields: { "applicantsCount": "$meta.applicantsCount"}},
{$project:{meta:0}}])