1
文檔 - 文檔由人口數據,文字數據組成。如何在mongodb聚合的最後階段減少到單個文檔?
{
"_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這將有助於創建一個包含有關標識,面積,密度,男數據和女性的數據信息單一的文件(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);
}
});
});
http://stackoverflow.com/a/40732823/2683814看看這是否有幫助。 – Veeram