Skip to content

Instantly share code, notes, and snippets.

@banker
Created March 21, 2012 19:52
Show Gist options
  • Save banker/2152039 to your computer and use it in GitHub Desktop.
Save banker/2152039 to your computer and use it in GitHub Desktop.
> db.f.save({u: 1, v: 2})
> db.f.save({u: 2, v: 2})
> db.f.save({u: 1, v: 5})
> db.f.save({u: 2, v: 10})
> m = function() { emit(this.u, this.v) }
function () {
emit(this.u, this.v);
}
> r = function(k, vs) { r = 0; vs.forEach(function(v) { r += v; }); return r; }
function (k, vs) {
r = 0;
vs.forEach(function (v) {r += v;});
return r;
}
> db.g.mapReduce(m, r, out: 'agg')
> db.agg.find()
{ "_id" : 1, "value" : 7 }
{ "_id" : 2, "value" : 12 }
> db.agg.update({_id: 1}, {$set: {a: 100}})
> db.agg.find()
{ "_id" : 2, "value" : 12 }
{ "_id" : 1, "a" : 100, "value" : 7 }
> db.f.mapReduce(m, r, {out: {reduce: 'agg'}})
{
"result" : "agg",
"timeMillis" : 2,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 3
},
"ok" : 1,
}
> db.agg.find()
{ "_id" : null, "value" : null }
{ "_id" : 2, "value" : 24 }
{ "_id" : 1, "value" : 107 }
// "value" should not be 107.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment