with nodejs mongoose odm
var d = 1 var rand = Math.random(); var question_count = models.question.count({ difficulty: d}, function(err, count) { if(err) { console.log(err); return; } models.question.findOne({ difficulty: d }).limit(-1).skip( rand * count).exec(function(err, doc) { if(err) { console.log(err); return; } if(doc) { res.respond(doc); } else { res.respond(404); } }); }); |