【问题标题】:How to apply $lookup with conditions in mongodb?如何在 mongodb 中应用 $lookup 条件?
【发布时间】:2020-05-11 04:37:15
【问题描述】:

我正在使用 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' },
})

另一个collection 像这样命名为Referrallinks

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 的推荐。

**注意:**我也想拥有 categorycompany 的对象并尝试了这种聚合,但它给出了错误

{
  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"
}

我怎样才能做到这一点?

【问题讨论】:

    标签: mongodb mongoose nosql aggregation-framework lookup


    【解决方案1】:

    使用$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"
        }
    }])
    

    【讨论】:

    • 您的查询中有一些错误。在此处查看错误imgur.com/6tRCUuv
    • @ahsan 啊是的,$expr 丢失,请重试
    • 我可以同时拥有populate$lookup 吗?就像我有companyId 作为category 集合的(ObjectId),我想在我的结果中也有category
    • @ahsan 在这种情况下你必须继续使用$lookup
    • 我试过了,但运气不好。你能修改你的答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2019-02-04
    • 2020-11-22
    • 1970-01-01
    • 2021-02-22
    • 2019-06-20
    相关资源
    最近更新 更多