-
-
Save rueckstiess/a53d9d7b29b69a7455e6 to your computer and use it in GitHub Desktop.
MongoDB shell helper for reservoir sampling via map/reduce
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
if (typeof DBCollection !== 'undefined') { | |
DBCollection.prototype.sample = function(num) { | |
function mapFn() { | |
if (i < k) { | |
emit(i, this); | |
} else { | |
var j = Math.floor(Math.random() * i); | |
if (j < k) { | |
emit(j, this); | |
} | |
} | |
i++; | |
} | |
function reduceFn(key, value) { | |
return value[value.length - 1]; | |
} | |
// global scope | |
var scope = { | |
k: num || 100, | |
i: 0 | |
}; | |
var res = this._db.runCommand({ | |
mapReduce: this._shortName, | |
map: mapFn, | |
reduce: reduceFn, | |
out: { | |
inline: 1 | |
}, | |
scope: scope, | |
}); | |
return res.results.map(function(d) { | |
return d.value; | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Start the mongo shell like this:
You can also add the following line to your
~/.mongorc.js
file to always load the file on shell startup (unless started with--norc
):In the mongo shell, you can now do:
This gives you 50 random documents from the
users
collection.