如何使用 mongodb 中的条件应用 $lookup?

How to apply $lookup with conditions in mongodb?

我正在使用 mongodb 数据库并想在 2 collections 上应用 $lookup 但有特定条件。 我有一个 collection 命名为 Company 像这样

new Schema({
    name: String,
    benefit: String,
    benefitDesc: String,
    company_url: String,
    logoUrl: String,
    coverUrl: String,
    desc: String,
    createdAt: String,
    categoryId: { type: Schema.Types.ObjectId, ref: 'categories' },
})

还有一个叫 Referrallinkscollection 像这样

new Schema({
    referral_link: String,
    referral_code: String,
    isLink: Number,
    offer_name: String,
    offer_desc: String,
    user_email: String,
    companyId: { type: Schema.Types.ObjectId, ref: 'companies' },
    addedByAdmin: { type: Boolean, default: true },
    number_of_clicks: Number,
    referral_country: String,
    link_status: String,
    categoryId: { type: Schema.Types.ObjectId, ref: 'categories' },
    number_of_clicks: { type: Number, default: 0 },
    createdAt: String,
    updatedAt: String,
    userId: { type: Schema.Types.ObjectId, ref: 'users' }
})

现在当我应用这个时 $lookup

Company.aggregate([{
    $lookup: {
        from: 'referrallinks',
        localField: '_id',
        foreignField: 'companyId',
        as: 'referrals'
    }
}])

所以我让所有 referrals 的公司都像这样 array

[
  {name : '', benefit : '', benefiDesc : '', referrals : []},
  {name : '', benefit : '', benefiDesc : '', referrals : []},
  {name : '', benefit : '', benefiDesc : '', referrals : []},
  .....
]

但我想在每个 company's referrals 数组中只有具有 link_statusApproved 的引用。

**注意:**我也想 category 对象与 company 并尝试了这个聚合但它给出了错误

{
  from: "referrallinks",
  let: { company_id: "$_id" },
  pipeline: [{
    $match: {
      $expr: {
       $and: [
        { $eq: [ "$$company_id", "$companyId" ] },
        { $eq: [ "$link_status", "Approved" ] }
       ]
     }
    },
  {
    $lookup : {
    from : "categories",
    let  : {"category_id" : "$categoryId"},
    pipeline : [
      {
        $match : {
          $expr : {
            $eq : ["category_id","$_id"]
          }
        }
      }
    ],
    as : "company"
   }
  }
 }],
 as: "referrals"
}

我怎样才能做到这一点?

使用$lookup with custom pipeline指定附加过滤条件:

Company.aggregate([{
    $lookup: {
        from: "referrallinks",
        let: { company_id: "$_id" },
        pipeline: [{
            $match: {
                $expr: {
                    $and: [
                        { $eq: [ "$$company_id", "$companyId" ] },
                        { $eq: [ "$link_status", "Approved" ] }
                    ]
                }
            }
        }],
        as: "referrals"
    }
}])

编辑:运行 嵌套 $lookup 你的代码应该如下所示:

Company.aggregate([{
    $lookup: {
        from: "referrallinks",
        let: { company_id: "$_id" },
        pipeline: [
            {
                $match: {
                    $expr: {
                        $and: [
                            { $eq: [ "$$company_id", "$companyId" ] },
                            { $eq: [ "$link_status", "Approved" ] }
                        ]
                    }
                }
            },
            {
                $lookup: {
                    from: "categories",
                    localField: "categoryId",
                    foreignField: "_id",
                    as: "company"
                }
            }
        ],
        as: "referrals"
    }
}])