如何在mongodb聚合的finall阶段减少到单个文档?

时间:2016-12-17 05:40:17

标签: mongodb mongodb-query aggregation-framework

文档 - 文件由人口数据,文盲数据组成。

    { 
    "_id" : NumberInt(0), 
    "area" : "India", 
    "population" : {
        "density" : NumberInt(382), 
        "class" : [
            {
                "rural" : [
                    {
                        "male" : [
                            NumberInt(61285192), 
                            NumberInt(427917052)
                        ]
                    }, 
                    {
                        "female" : [
                            NumberInt(56300322), 
                            NumberInt(405170610)
                        ]
                    }
                ]
            }, 
            {
                "urban" : [
                    {
                        "male" : [
                            NumberInt(21666943), 
                            NumberInt(195807196)
                        ]
                    }, 
                    {
                        "female" : [
                            NumberInt(19536830), 
                            NumberInt(181298564)
                        ]
                    }
                ]
            }
        ]
    }, 
    "education" : {
        "class" : [
            {
                "rural" : [
                    {
                        "male" : NumberInt(288047480)
                    }, 
                    {
                        "female" : NumberInt(204973398)
                    }
                ]
            }, 
            {
                "urban" : [
                    {
                        "male" : NumberInt(204973398)
                    }, 
                    {
                        "female" : NumberInt(129276960)
                    }
                ]
            }
        ]
    }
}

汇总查询 试图找出nodejs查询,这将有助于创建包含有关id,区域,密度,男性数据和女性数据的信息的单个文档(JSON)。 下面的查询生成两个文档。如何将两个文档合并为单个文档

router.get('/population/:id', function (req, res, next) {
        var id = Number(req.params.id);
        sCollection.aggregate(
            {
                $match: { _id: id }
            },
            {
                $unwind: '$population.class'
            },
            {
                $unwind: '$population.class.rural'
            },
            {
                $project: { _id: '$_id', area: '$area', density: '$population.density', ruralMale: '$population.class.rural.male', ruralFemale: '$population.class.rural.female' }
            },
           ).toArray(function (err, population) {
                if (err) {
                    res.send('Error census-population not found  ' + err);
                } else {

                    res.json(population);
                }
            });
    });

Overall Aggregation Flow - 附图将有助于更好地理解问题

1 个答案:

答案 0 :(得分:0)

你需要像这样使用$ group:

[ 
   {$match: { _id: 0 }},
   {$unwind: '$population.class'},
   {$unwind: '$population.class.rural'},  
   {$group:{_id:"$_id", area: {$first: "$area"}, density: {$first:'$population.density'}, ruralMale: {$first: '$population.class.rural.male'}, ruralFemale: {$last:'$population.class.rural.female' }}}