This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crimes = [ | |
{"crime": "Assault", "property": "States Avenue"}, | |
{"crime": "Larceny", "property": "Marvin Gardens"}, | |
{"crime": "Assault", "property": "States Avenue"}, | |
{"crime": "Motor Vehicle Theft", "property": "Boardwalk"}, | |
{"crime": "Assault", "property": "States Avenue"}, | |
{"crime": "Larceny", "property": "Marvin Gardens"}, | |
{"crime": "Assault", "property": "States Avenue"}, | |
{"crime": "Drugs", "property": "Marvin Gardens"}, | |
{"crime": "Drugs", "property": "Marvin Gardens"}, | |
{"crime": "Larceny", "property": "Marvin Gardens"}, | |
{"crime": "Larceny", "property": "Marvin Gardens"}, | |
{"crime": "Motor Vehicle Theft", "property": "Boardwalk"}, | |
{"crime": "Motor Vehicle Theft", "property": "Boardwalk"}, | |
{"crime": "Drugs", "property": "Marvin Gardens"} | |
] | |
if(db.crimes.count() == 0) { | |
crimes.forEach(function(c) { | |
db.crimes.insert(c) | |
}) | |
} | |
function map() { | |
emit(this.property, 1) | |
} | |
function reduce(propKey, vals) { | |
var sum = 0 | |
vals.forEach(function(v) { | |
sum += v | |
}) | |
return sum | |
} | |
var mr = db.crimes.mapReduce(map, reduce), | |
results = db[mr.result].find() | |
printjson(result.toArray()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment