0
以下查詢將計算每個國家/地區的GDP總和。 該查詢會查看每個國家/地區。 但可以說,我要修改此算法中:如何限制給定集合的mongodb聚合
Find gdp for a given subset of countries {BR, CN and UK} only
這張照片被記入YouTube視頻在這裏: https://www.youtube.com/watch?v=8xiA4i4eepE
以下查詢將計算每個國家/地區的GDP總和。 該查詢會查看每個國家/地區。 但可以說,我要修改此算法中:如何限制給定集合的mongodb聚合
Find gdp for a given subset of countries {BR, CN and UK} only
這張照片被記入YouTube視頻在這裏: https://www.youtube.com/watch?v=8xiA4i4eepE
如果您想通過country
和,以濾除sum(GDP)
然後分組之前增加一個比賽階段。例如:
db.world_gdp.aggregate([
// focus on BR, CN and UK countries ... the subsequent stages in the
// pipeline will only get docs for these countries
{ $match: { country: { $in: ['BR', 'CN', 'UK'] } } },
// group by country and sum GDP
{ $group: { _id: "$country", gdp: { $sum: "$gdp"} } },
// restrict to those countries having sum(GDP) >= 2500
{ $match: { gdp: { $gte: 2500 } } },
// sort by sum(GDP)
{ $sort: { gdp: -1 } }
])
或者,如果你有興趣在過濾通過country
,而不是通過過濾sum(GDP)
然後試試這個:
db.world_gdp.aggregate([
// focus on BR, CN and UK countries ... the subsequent stages in the
// pipeline will only get docs for these countries
{ $match: { country: { $in: ['BR', 'CN', 'UK'] } } },
// group by country and sum GDP
{ $group: { _id: "$country", gdp: { $sum: "$gdp"} } },
// sort by sum(GDP)
{ $sort: { gdp: -1 } }
])
你能後你一直在運行,請查詢? – robjwilkins