Skip to content

Instantly share code, notes, and snippets.

@caged
Created December 11, 2010 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caged/737033 to your computer and use it in GitHub Desktop.
Save caged/737033 to your computer and use it in GitHub Desktop.
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