How to restrict mongodb aggregation for a given set -


the following query compute sum of gdp per country. query through each country. lets want modify algo:

find gdp given subset of countries {br, cn , uk} only

how can done in mongodb ?enter image description here

this picture credited youtube video here: https://www.youtube.com/watch?v=8xia4i4eepe

if want filter country and sum(gdp) add match stage before grouping. example:

db.world_gdp.aggregate([    // focus on br, cn , uk countries ... subsequent stages in   // pipeline docs these countries   { $match: { country: { $in: ['br', 'cn', 'uk'] } } },    // group country , sum gdp   { $group: { _id: "$country", gdp: { $sum: "$gdp"} } },    // restrict countries having sum(gdp) >= 2500   { $match: { gdp: { $gte: 2500 } } },    // sort sum(gdp)   { $sort: { gdp: -1 } }  ]) 

or, if interested in filtering country instead of filtering sum(gdp) try this:

db.world_gdp.aggregate([    // focus on br, cn , uk countries ... subsequent stages in   // pipeline docs these countries   { $match: { country: { $in: ['br', 'cn', 'uk'] } } },    // group country , sum gdp   { $group: { _id: "$country", gdp: { $sum: "$gdp"} } },    // sort sum(gdp)   { $sort: { gdp: -1 } }  ]) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -