如何在猫鼬中组合嵌套的$ lookup 3级别?

时间:2019-06-20 02:10:14

标签: node.js mongodb mongoose

我想将单独的集合组合到1个json,但是在嵌套$ lookup中有一些问题。

这是我的示例集合。 集合Package_subscriptions:

[
    {
        "_id": "1",
        "package_name": "Small",
        "package_desc": "AAA",
        "package_price": 10
    },
    {
        "_id": "2",
        "package_name": "Medium",
        "package_desc": "BBB",
        "package_price": 20    
    }

集合Package_modules:

{
    "_id" : 1,
    "subscription_id" : 1,
    "billing_model_id" : 1,
    "module_id" : 1,
    "created_at" : ISODate("2019-06-17T07:59:43.199Z"),
}
{
    "_id" : 2,
    "subscription_id" : 1,
    "billing_model_id" : 3,
    "module_id" : 2,
    "created_at" : ISODate("2019-06-17T08:00:37.464Z"),
}
{
    "_id" : 3,
    "subscription_id" : 2,
    "billing_model_id" : 2,
    "module_id" : 1,
    "created_at" : ISODate("2019-06-17T08:00:56.610Z"),
}
{
    "_id" : 4,
    "subscription_id" : 2,
    "billing_model_id" : 4,
    "module_id" : 2,
    "created_at" : ISODate("2019-06-17T08:01:29.667Z"),
}

收集模块:

{
    "_id" : 1,
    "module_name" : "Call",
    "status" : "Active",
}
{
    "_id" : 2,
    "module_name" : "SMS",
    "status" : "Active",
}

集合Billing_models

{
    "_id" : 1,
    "unit_count" : "Menit",
    "counter" : 2000,
},
{
    "_id" : 2,
    "unit_count" : "Menit",
    "counter" : 3000,
}
{
    "_id" : 3,
    "unit_count" : "SMS",
    "counter" : 3000,
},
{
    "_id" : 4,
    "unit_count" : "SMS",
    "counter" : 5000,
}

这是我尝试解决此问题的代码,但是没有达到预期的效果。

Package_subscription
            .aggregate([
                {
                    $lookup: {
                            from: "o_package_modules",
                            localField: "_id",
                            foreignField: "subscription_id",
                            as: "package_modules"
                    }
                },
                {
                    $unwind: {
                        path: "$package_modules",
                        preserveNullAndEmptyArrays: true                        
                    }
                },
                {
                    $lookup: {
                        from: "o_modules",
                        localField: "package_modules.module_id",
                        foreignField: "_id",
                        as: 'module'
                    }
                },
                {
                    $lookup: {
                        from: "o_billing_models",
                        localField: "package_modules.billing_model_id",
                        foreignField: "_id",
                        as: 'billing_module'
                    }
                }
            ])            
            .exec()                        
            .then((pricing) => {                
                res.json(pricing)                
            })
            .catch((err) => {
                res.send(err)
            })

我希望输出是:

[
    {
        "_id": "1",
        "package_name": "Small",
        "package_desc": "AAA",
        "package_price": 10,        
        "package_modules": [
            {
                "_id": 1,
                "subscription_id": 1,
                "billing_model": {
                        "_id": 1,
                        "unit_count": "Menit",
                        "counter": 2000
                    },
                "module": {
                        "_id": 1,
                        "module_name": "Call",
                        "status": "Active"
                    },
                "created_at": "2019-06-17T07:59:43.199Z",
            },
            {
                "_id": 2,
                "subscription_id": 1,
                "billing_model": {
                        "_id": 3,
                        "unit_count": "SMS",
                        "counter": 3000
                    },
                "module": {
                        "_id": 2,
                        "module_name": "SMS",
                        "status": "Active"
                    },
                "created_at": "2019-06-17T08:00:37.464Z",
            }
        ]
    },
    {
        "_id": 2,
        "package_name": "Medium",
        "package_desc": "BBB",
        "package_price": 20,
        "package_modules": [
            {
                "_id": 3,
                "subscription_id": 2,
                "billing_model": {
                        "_id": 2,
                        "unit_count": "Menit",
                        "counter": 3000
                    },
                "module": {
                        "_id": "1",
                        "module_name": "Call",
                        "status": "Active"
                    },
                "created_at": "2019-06-17T08:01:29.667Z",
            },
            {
                "_id": 4,
                "subscription_id": 2,
                "billing_model": {
                        "_id": 4,
                        "unit_count": "SMS",
                        "counter": 5000
                    },
                "module": {
                        "_id": 2,
                        "module_name": "SMS",
                        "status": "Active"
                    },
                "created_at": "2019-06-17T08:01:50.285Z",
            }
        ]
    }
]

但是我的代码输出是:

[
    {
        "_id": "1",
        "package_name": "Small",
        "package_desc": "AAA",
        "package_price": 10,        
        "package_modules": {
                "_id": 1,
                "subscription_id": 1,
                "billing_model_id": 1
                "module_id": 1                 
                "created_at": "2019-06-17T07:59:43.199Z",
            },            
        },
        "billing_model": [
            {
                "_id": 1,
                "unit_count": "Menit",
                "counter": 2000
            },
        ],

        "module": [
            {
                "_id": 1,
                "module_name": "Call",
                "status": "Active"
            }
        ]
    },
    {
        "_id": "1",
        "package_name": "Small",
        "package_desc": "AAA",
        "package_price": 10,        
        "package_modules": {
                "_id": 2,
                "subscription_id": 2,
                "billing_model_id": 3
                "module_id": 2                 
                "created_at": "2019-06-17T07:59:43.199Z",
            },            
        },
        "billing_model": [
            {
                "_id": 3,
                "unit_count": "SMS",
                "counter": 3000
            },
        ],

        "module": [
            {
                "_id": 2,
                "module_name": "SMS",
                "status": "Active"
            }
        ]
    }
    ..........
    // And medium where is loop again
]

1 个答案:

答案 0 :(得分:0)

我认为您的代码中存在多个问题。

  1. 在您的$lookup阶段,您需要将as字段设置为package_modules.billing_modulepackage_modules.module,而不是billing_module和{{1 }}等,以使其成为module对象的字段,而不是单独的对象本身。

  2. 您需要在每个package_module阶段之后unwind,因为$lookup返回一个数组而不是对象。

  3. 您需要在$lookup管道的末尾$group来将一个订阅的所有aggregation push合并到一个数组中。

    < / li>

解决所有上述问题后, 您的聚合管道应如下所示:

package_modules

最后一个Package_subscription .aggregate([ { $lookup: { from: "o_package_modules", localField: "_id", foreignField: "subscription_id", as: "package_modules" } }, { $unwind: { path: "$package_modules", preserveNullAndEmptyArrays: true } }, { $lookup: { from: "o_modules", localField: "package_modules.module_id", foreignField: "_id", as: 'package_modules.module' } }, { $unwind: { path: "$package_modules.module", preserveNullAndEmptyArrays: true } }, { $lookup: { from: "o_billing_models", localField: "package_modules.billing_model_id", foreignField: "_id", as: 'package_modules.billing_module' } }, { $unwind: { path: "$package_modules.billing_module", preserveNullAndEmptyArrays: true } }, { $group: { _id: "$_id", package_modules : {$push : "$package_modules"}, package_name: {$first . : "$package_name"}, package_desc: {$first . : "$package_desc"}, package_price: {$first . : "$package_price"} } } ]) .exec() .then((pricing) => { res.json(pricing) }) .catch((err) => { res.send(err) }) 阶段的$push将所有$group合并为一个数组。我认为那是您最后想要的。

我希望这对您有用。