我如何在Mongodb中使用$ lookup获取引用的dcoument作为嵌套文档? [英] How can i get the referenced dcoument as nested document with $lookup in Mongodb?

查看:75
本文介绍了我如何在Mongodb中使用$ lookup获取引用的dcoument作为嵌套文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongodb数据库,并想在2个collections上应用$lookup,但是要有特定条件.我有一个名为Company的收藏集

I am using mongodb databases and want to apply $lookup on 2 collections but with specific conditions. I have one collection named Company like this

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' },
})

另一个名为Referrallinks的集合

And another collection named Referrallinks like this

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",
        let: { company_id: "$_id" },
        pipeline: [{
                $match: {
                    $expr: {
                        $and: [
                            { $eq: ["$$company_id", "$companyId"] },
                            { $eq: ["$link_status", "Approved"] }
                        ]
                    }
                }
            },
            {
                $lookup: {
                    from: "categories",
                    localField: "categoryId",
                    foreignField: "_id",
                    as: "category"
                }
            }
        ],
        as: "referrals"
    }
}])

所以我在每个公司的referral对象的key中都包含了category对象.但是我想要它在这样的referral对象之外

So i am getting category object inside each company's referral object's key. But i want it outside of referral Object like this

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

我该怎么做?

推荐答案

您只需要将嵌套的$lookup从第一个中移出并作为单独的管道步骤运行即可:

You just need to move your nested $lookup out of first one and run as a separate pipeline step:

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

请注意,referrals.categoryId的计算结果为ObjectId个值的数组

Note that referrals.categoryId evaluates to an array of ObjectId values

蒙戈游乐场

这篇关于我如何在Mongodb中使用$ lookup获取引用的dcoument作为嵌套文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆