node.js - 如何在 mongoose 中组合嵌套 $lookup 3 层?

标签 node.js mongodb mongoose

我想将单独的集合合并为 1 个 json,但嵌套 $lookup 时遇到一些问题。

这是我的示例集合。 收藏包_订阅:

[
    {
        "_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. $lookup 阶段,您需要将 as 字段设置为 package_modules.billing_modulepackage_modules.module,而不是 billing_modulemodule 等,以便它成为 package_module 对象的字段,而不是单独的对象本身。

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

  3. 您需要在聚合管道末尾处$group将一个订阅的所有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)
        })

最后一个$group阶段中的$push会将所有package_modules合并到一个数组中。我认为这就是您最终想要的。

我希望这对你有用。

关于node.js - 如何在 mongoose 中组合嵌套 $lookup 3 层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56678088/

相关文章:

javascript - MongoDB/Mongoose 查询生成器

node.js - Nodejs Mongoose - 一次数据库调用 = 两个按不同标准排序的独立数组

mongodb - Mongoose 填充数组中的单个项目

angularjs - Angular CRUD 从模式中删除行

javascript - 访问嵌套在数组内的 JSON 对象

javascript - 不成功的 POST 请求处理

database - Golang bson.E 未由包 bson 声明

javascript - 在 NodeJS 中从 MP4 中提取 RGB 帧数据

mongodb - 如何编辑 Mongodb.conf 文件

MongoDB 聚合查找