I'm newbie of Sails JS framwork and I'm trying to write a small quiz app. Each quiz, my app will select 6 random questions from question collection.
Is it possible in SailsJS 0.11, MongoDB 3.6.8? How can I do that?
Many thanks
I'm newbie of Sails JS framwork and I'm trying to write a small quiz app. Each quiz, my app will select 6 random questions from question collection.
Is it possible in SailsJS 0.11, MongoDB 3.6.8? How can I do that?
Many thanks
You can call find
method with skip
and limit
criteria.
Question
.count()
.then(count => Question.find().limit(6).skip(parseInt(Math.random() * count)))
.then(questions => questions.sort(() => 0.5 - Math.random()))
.then(questions => doSomethingWith(questions))
.catch(sails.log.error);