I am new with MongoDB and Map Reduce. I have cities, states and population for each city.
The question is to find the largest state population. I have done a query to get all states with their population but I could not get only the max state with its population
Map
map = function(){emit(this.state , this.pop)}
Reduce
reduce = function (key, values) {
var max = 1;
var pop = Array.sum(values);
if (pop > max) max = pop;
return pop;
}
to run it
db.zipcodes.mapReduce(map , reduce , {out:'result1'}).find()