如何将子文档$ group到单个数组?

时间:2019-06-21 00:57:14

标签: node.js mongodb mongoose aggregation-framework

我有单独的收藏夹,并且成功地将文档分组,但是有一个问题。程序包模块出现2次,但具有不同的程序包子模块,我希望程序包模块仅出现1次,而程序包子模块位于对象数组中,而不是1个对象。

这是文档,看看package_submodules,有相同的package_module_id:5d074931074cc830be64d665d,我希望对象数组中只有一个package_submodule,而不是像这样分开。

[
    {
        "_id": "5d074704e155d703b69086aa",
        "package_name": "Large",
        "package_desc": "Facere rem nostrum ipsam eligendi voluptatem reprehenderit sint quia ea. Quisquam amet illum omnis est amet in sit. Dicta nesciunt sed voluptas sed. Facilis aut dolores quasi impedit eaque velit. Blanditiis et corporis vitae ea id voluptatem consectetur quia unde. Dolorem sed aut.\n \rCupiditate assumenda animi dignissimos vero quibusdam esse perferendis perferendis laboriosam. Architecto et sit et deserunt. Numquam aliquid sed modi porro ea fugit vitae dolore expedita.\n \rOdio aut repellendus nihil. Est rerum qui dolorum temporibus eius recusandae odio aut. Placeat perspiciatis qui eos ut aliquid voluptas officia. Consectetur vitae officia illo facilis iusto et rerum. Quia et ipsa.",
        "package_price": 15000000,
        "package_modules": [
            {
                "_id": "5d074931074c830be64d665d",
                "subscription_id": "5d074704e155d703b69086aa",
                "billing_model_id": "5d0747be27cb8d06b6480aa1",
                "module_id": "5d074775c8b6cf0541b02993",
                "created_at": "2019-06-17T08:02:57.388Z",
                "__v": 0,
                "module": {
                    "_id": "5d074775c8b6cf0541b02993",
                    "module_name": "Call",
                    "status": "Active",
                    "created_at": "2019-06-17T07:55:33.916Z",
                    "__v": 0
                },
                "billing_module": {
                    "_id": "5d0747be27cb8d06b6480aa1",
                    "unit_count": "Menit",
                    "counter": 8000,
                    "created_at": "2019-06-17T07:56:46.511Z",
                    "__v": 0
                },
                "package_submodules": {
                    "_id": "5d0baa68955693648d4e6892",
                    "package_module_id": "5d074931074c830be64d665d",
                    "sub_module_id": "5d074a1925e5000eff15272b",
                    "created_at": "2019-06-17T08:13:59.419Z"
                }
            },
            {
                "_id": "5d074931074c830be64d665d",
                "subscription_id": "5d074704e155d703b69086aa",
                "billing_model_id": "5d0747be27cb8d06b6480aa1",
                "module_id": "5d074775c8b6cf0541b02993",
                "created_at": "2019-06-17T08:02:57.388Z",
                "__v": 0,
                "module": {
                    "_id": "5d074775c8b6cf0541b02993",
                    "module_name": "Call",
                    "status": "Active",
                    "created_at": "2019-06-17T07:55:33.916Z",
                    "__v": 0
                },
                "billing_module": {
                    "_id": "5d0747be27cb8d06b6480aa1",
                    "unit_count": "Menit",
                    "counter": 8000,
                    "created_at": "2019-06-17T07:56:46.511Z",
                    "__v": 0
                },
                "package_submodules": {
                    "_id": "5d0baa78955693648d4e6893",
                    "package_module_id": "5d074931074c830be64d665d",
                    "sub_module_id": "5d074a27fa83a80f608a153b",
                    "created_at": "2019-06-17T08:13:59.419Z"
                }
            },
            .....
        ]
    }
......
]

这是我尝试过的一些代码和一些帮助:

Package_subscription
            .aggregate([
                {
                    $sort: {
                        package_name: 1
                    }
                },
                {
                    $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                        
                    }
                },
                {
                    $lookup: {
                        from: "o_package_submodules",
                        localField: "package_modules._id",
                        foreignField: "package_module_id",
                        as: 'package_modules.package_submodules'
                    }
                },
                {
                    $unwind: {
                        path: "$package_modules.package_submodules",
                        preserveNullAndEmptyArrays: true                        
                    }
                }, 
                {
                    $lookup: {
                        from: "o_submodules",
                        localField: "package_modules.package_submodules.submodule_id",
                        foreignField: "_id",
                        as: 'package_modules.package_submodules.submodule'
                    }
                },
                {
                    $unwind: {
                        path: "$package_modules.package_submodules.submodule",
                        preserveNullAndEmptyArrays: true                        
                    }
                }, 
                {
                    $group: {
                        _id: "$_id",
                        package_name: {$first: "$package_name"},
                        package_desc: {$first: "$package_desc"},
                        package_price: {$first: "$package_price"},
                        package_modules : {$push: "$package_modules"}                        
                    }
                }
            ])            
            .exec()                        
            .then((pricing) => {                
                res.json(pricing)                
            })
            .catch((err) => {
                res.send(err)
            })

如果在$ group阶段,则添加如下新字段:

$group: {
            _id: "$_id",
            package_name: {$first: "$package_name"},
            package_desc: {$first: "$package_desc"},
            package_price: {$first: "$package_price"},
            package_modules : {$push: "$package_modules"} ,
            "package_modules.package_submodules": {$push: "$package_modules.package_submodules"}
            }
        }

然后出现错误“ errmsg”:“字段名称'package_modules.package_submodules'不能包含'。'”,

如何将子文档$ group分成单个数组,就像我期望的那样:

[
    {
        "_id": "5d074704e155d703b69086aa",
        "package_name": "Large",
        "package_desc": "Facere rem nostrum ipsam eligendi voluptatem reprehenderit sint quia ea. Quisquam amet illum omnis est amet in sit. Dicta nesciunt sed voluptas sed. Facilis aut dolores quasi impedit eaque velit. Blanditiis et corporis vitae ea id voluptatem consectetur quia unde. Dolorem sed aut.\n \rCupiditate assumenda animi dignissimos vero quibusdam esse perferendis perferendis laboriosam. Architecto et sit et deserunt. Numquam aliquid sed modi porro ea fugit vitae dolore expedita.\n \rOdio aut repellendus nihil. Est rerum qui dolorum temporibus eius recusandae odio aut. Placeat perspiciatis qui eos ut aliquid voluptas officia. Consectetur vitae officia illo facilis iusto et rerum. Quia et ipsa.",
        "package_price": 15000000,
        "package_modules": [
            {
                "_id": "5d074931074c830be64d665d",
                "subscription_id": "5d074704e155d703b69086aa",
                "billing_model_id": "5d0747be27cb8d06b6480aa1",
                "module_id": "5d074775c8b6cf0541b02993",
                "created_at": "2019-06-17T08:02:57.388Z",
                "__v": 0,
                "module": {
                    "_id": "5d074775c8b6cf0541b02993",
                    "module_name": "Call",
                    "status": "Active",
                    "created_at": "2019-06-17T07:55:33.916Z",
                    "__v": 0
                },
                "billing_module": {
                    "_id": "5d0747be27cb8d06b6480aa1",
                    "unit_count": "Menit",
                    "counter": 8000,
                    "created_at": "2019-06-17T07:56:46.511Z",
                    "__v": 0
                },
                "package_submodules": [
                    {
                        "_id": "5d0baa68955693648d4e6892",
                        "package_module_id": "5d074931074c830be64d665d",
                        "sub_module_id": "5d074a1925e5000eff15272b",
                        "created_at": "2019-06-17T08:13:59.419Z"
                    },
                    {
                        "_id": "5d0baa78955693648d4e6893",
                        "package_module_id": "5d074931074c830be64d665d",
                        "sub_module_id": "5d074a27fa83a80f608a153b",
                        "created_at": "2019-06-17T08:13:59.419Z"
                    },
            },            
            .....
        ]
    }
......
]

0 个答案:

没有答案