Skip to content

Instantly share code, notes, and snippets.

@svkerr
Created October 6, 2012 19:28
Show Gist options
  • Save svkerr/3845860 to your computer and use it in GitHub Desktop.
Save svkerr/3845860 to your computer and use it in GitHub Desktop.
mapf='''function() {
...: emit(this.admin1_code,
...: {count:1, pop:this.population, elev:this.elevation});
...: }'''
finalizef = '''function(key,doc) {
....: return {
....: count:doc.count,
....: pop: doc.pop,
....: mean_pop: doc.pop/doc.count,
....: mean_elev: doc.elev/doc.count};
....: }'''
db.cities.reduce(
....: map=mapf,
....: reduce=reducef,
....: out='state_pop_elev',
....: query={'country_code': 'US'},
....: finalize=finalizef)
And here is a document from the db.state_pop_elev collection:
{u'_id': u'WY', u'value': {u'count': 6.0, u'mean_pop': 35860.833333333336, u'mean_elev': 1679.3333333333333, u'pop': 215165.0}}
@svkerr
Copy link
Author

svkerr commented Oct 6, 2012

I then try this:
cursor = db.state_pop_elev.find()

In [60]: for city in cursor.sort('mean_elev'):
....: print city

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment