Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Created March 17, 2015 21:35
Show Gist options
  • Save ben-bradley/a1d7d21cfd3bf15ba8b8 to your computer and use it in GitHub Desktop.
Save ben-bradley/a1d7d21cfd3bf15ba8b8 to your computer and use it in GitHub Desktop.
Select a random document with Mongoose
function findRandom(limit) {
return Promise(function(resolve, reject) {
Model.count({}, function(err, count) {
var skip = _.random(0, count - limit);
Model.find().skip(skip).limit(limit).exec(function(err, docs) {
if (err)
return reject(err);
resolve(docs);
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment