Skip to content

Instantly share code, notes, and snippets.

@JanPaulEttles
Created March 12, 2012 11:53
Show Gist options
  • Select an option

  • Save JanPaulEttles/2021377 to your computer and use it in GitHub Desktop.

Select an option

Save JanPaulEttles/2021377 to your computer and use it in GitHub Desktop.
scope3
DBCollection coll = db.getCollection("collection");
String map = "function() {" +
"if(this.arrayOfStuff) {" +
"this.arrayOfStuff.forEach(function(stuff) {" +
"if(stuff.date > from && stuff.date < to) {" +
"var date = Date.UTC(stuff.date.getFullYear(), stuff.date.getMonth(), stuff.date.getDate(), stuff.date.getHours());" +
"emit({day: date}, {count:1});" +
"}" +
"});" +
"}" +
"};";
String reduce = "function(key , values) {" +
"var total = 0;" +
"values.forEach(function(v) {" +
"total += v.count;" +
"});" +
"return {count : total};" +
"};";
DBObject query = new BasicDBObject();
query.put("meh", meh);
MapReduceCommand cmd = new MapReduceCommand(coll, map, reduce, null, MapReduceCommand.OutputType.INLINE, query);
Map scope = new HashMap();
scope.put("from", from);
scope.put("to", to);
cmd.setScope(scope);
MapReduceOutput out = coll.mapReduce(cmd);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment