Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created December 3, 2012 10:52
Show Gist options
  • Select an option

  • Save h2rd/4194177 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/4194177 to your computer and use it in GitHub Desktop.
mongo get avg cities from NY and CA
use test
db.zips.aggregate({
$group : {
_id : { state: '$state', city: '$city'}
, totalPop : { $sum: '$pop' }
}
},{
$match : {
totalPop: {$gte : 25000},
$or: [{'_id.state': 'NY'}, {'_id.state': 'CA'}]
}
}, {
$group: {
_id : "$_id.state",
total: {$avg: '$totalPop'}
}
}, {
$group: {
_id: "Total",
total: {$avg: '$total'}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment