三个集合的聚合,从 job_posting 个拥有 notification_status 的用户集合获取工作是 true

Aggregation of three collections, Get jobs from job_posting collection of users those have notification_status is true

我有三个合集 1.user_details{字段: user_favorite_company_notification_status:boolean, user_id} user details collection 2.fav_companies{company_id, jobseeker_id} favorite companies collection 3.job_postings { company_id, enable_status, job_created_at} job postings collection。 我需要获取通知状态为 true 的用户最喜欢的公司 job_postings 的详细信息。

我尝试加入所有这些集合,以获取用户最喜欢的公司的职位。所以我需要获取所有通知状态为 true 的用户的详细信息,然后找到这些用户最喜欢的公司,然后为最喜欢的公司发布招聘信息。

  db.collection('user_details').aggregate([
        {$match : {user_favorite_company_notification_status: true}},
        {$lookup: {from : 'favorite_companies' ,localField : '_id', foreignField: 'jobseeker_id', as: 'fav_companies'}},
        { "$unwind": "$fav_companies" },
        {$lookup: {from : 'job_postings' , "pipeline":[
            {"$match":{
                $and:[
                    {company_id: "fav_companies.company_id"},
                    {enable_status: true},
                    {job_created_at: { $gt: previous_day}}
                    ]
                }
            }
            ], as: 'jobposts'}},
      ]).toArray((err, result)=>{
          console.log(result);
      })

Update your code with below one and check.

db.collection('user_details').aggregate([
    {$match : {user_favorite_company_notification_status: true}}, ,
    {$lookup: {from : 'favorite_companies' ,localField : '_id', foreignField: 'jobseeker_id', as: 'fav_companies'}},
    { "$unwind": "$fav_companies" },
    { 
        $lookup: {
            from: "job_postings",
            let: { company_id : "$fav_companies.company_id" },
            pipeline: [
                { 
                    $match:
                    { 
                        $expr:
                        { 
                            $and: [ 
                                    $eq : [{"$job_enable_status" : true}],
                                    $gt : [{"$job_created_at" : previous_day } ]
                                 ]

                        }
                    }
                }
            ],
            as: "fav_companies.jobposts"
        } 
    }
]).exec((err, result) => {
    console.log(result);
})
     db.collection('user_details').aggregate([
    {$match : {user_favorite_company_notification_status: true}},
    {$lookup: {from : 'favorite_companies' ,"let": { "user_id": "$_id" },
    "pipeline": [
        { "$match": {
            "$expr" : {
                "$and":[
                    {$eq:["$jobseeker_id","$$user_id"]},
                    {$eq:["$liked",true]}
                ]
            }
        } }
      ], as: 'fav_companies'}},
    { "$unwind": "$fav_companies"
     }
,
{$lookup: {from : 'job_postings' ,"let": { "companyId": "$fav_companies.company_id" },
"pipeline": [
    { "$match": {
        "$expr" : {
            "$and":[
                {$eq:["$company_id","$$companyId"]},
                {$eq:["$enable_status",true]},
                {$gt:["$job_created_at" , previous_day ] }

            ]
        }
    } }
  ], as: 'jobposts'}},
  { "$unwind": "$jobposts"
}