Mongodb多级查找未提供所需数据

时间:2019-11-18 06:57:34

标签: node.js mongodb mongoose aggregate

我对正在处理的项目的汇总查询感到困惑。这大概是我的数据的样子

shop = {
    _id: ObjectId('54265.....'),
    name: 'Shop 1',
    categories: ['856465....', '2453435...', '234543....']
}

categories = {
    _id: ObjectId('856465.....'),
    name: 'Category 1',
    type_one: ['5423456....', '2453435...', '234543....'],
    type_two: ['5423456....', '2453435...', '234543....'],
}

type_one = {
    _id: ObjectId('542345.....'),
    name: 'type 1'
}

type_two = {
    _id: ObjectId('5423456.....'),
    name: 'type 2'
}

我正在寻找的预期输出是

        {
            _id: ObjectId('54265.....'),
            name: 'Shop 1',
            categories: ['856465....', '2453435...', '234543....'],
            categoryData: [
                {
                    _id: ObjectId('856465.....'),
                    name: 'Category 1',
                    type_one: ['542345....', '2453435...', '234543....'],
                    type_two: ['5423456....', '2453435...', '234543....'],
                    type_oneData: [
                        {
                            _id: ObjectId('542345.....'),
                            name: 'type 1'
                        },
                        .....
                    ],
                    type_twoData: [
                        {
                            _id: ObjectId('5423456.....'),
                            name: 'type 2'
                        },
                        .....
                    ]
                },
                .....
            ]
        }

但是我现在可以得到的输出是

{
            _id: ObjectId('54265.....'),
            name: 'Shop 1',
            categories: ['5423456....', '2453435...', '234543....'],
            categoryData: 
                {

                    type_oneData: [
                        {
                            _id: ObjectId('542345.....'),
                            name: 'type 1'
                        },
                        .....
                    ],
                    type_twoData: [
                        {
                            _id: ObjectId('5423456.....'),
                            name: 'type 2'
                        },
                        .....
                    ]
                }
        }

我正在使用involues匹配,查找,展开...的mongodb查询...

shopData.aggregate(
    [
        { "$match": { _id: uniqueId } },

        {
            $lookup: {
                from: "categories",
                localField: "categories",
                foreignField: "_id",
                as: "categoryData"
            }
        },
        {
            $unwind: {
                path: "$categoryData",
                preserveNullAndEmptyArrays: true
            }
        },
        {
            $lookup: {
                from: "typeone",
                localField: "categoryData.type_one",
                foreignField: "_id",
                as: "categoryData.type_oneData"
            }
        },
        {
            $unwind: {
                path: "$categoryData.type_oneData",
                preserveNullAndEmptyArrays: true
            }
        },
        {
            $lookup: {
                from: "typetwo",
                localField: "categoryData.type_two",
                foreignField: "_id",
                as: "categoryData.type_twoData"
            }
        },
        {
            $unwind: {
                path: "$categoryData.type_twoData",
                preserveNullAndEmptyArrays: true
            }
        },

    ]
)

我无法遍历categoryData并收集信息,我的代码无法获取第一个对象的数据,而忽略了其余对象。我知道我错过了一些东西,但在过去的两天里我无法理解。 如何优化查询以获得所需的输出?任何帮助表示赞赏。

0 个答案:

没有答案