Skip to content

Instantly share code, notes, and snippets.

@adkron
Created May 24, 2010 02:29
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 adkron/411472 to your computer and use it in GitHub Desktop.
Save adkron/411472 to your computer and use it in GitHub Desktop.
map = function () {
emit(this.id, this.version);
}
reduce = function (key, values) {
return Math.max.apply(Math, values);
}
fin = function(key, value) {
return db.oplan.findOne({id:key, version:value});
}
db.oplan.mapReduce(map,reduce, {out:'versions', finalize:fin});
@adkron
Copy link
Author

adkron commented May 24, 2010

function (key, values) {
return db.oplan.findOne({id:key, version:Math.max.apply(Math, values)});
}

db.oplan.mapReduce(map,reduce, {out:'res'});
{
"result" : "res",
"timeMillis" : 12,
"counts" : {
"input" : 3,
"emit" : 3,
"output" : 2
},
"ok" : 1,
}
db.res.find();
{ "_id" : "1234A", "value" : { "_id" : ObjectId("4bf80c47e65228196db0a382"), "id" : "1234A", "version" : 1 } }
{ "_id" : "1234T", "value" : null }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment