Sails JS:从MongoDB中选择随机记录?

Sav*_*yen 3 mongodb node.js sails.js

我是Sails JS框架的新手,我正在尝试编写一个小测验应用程序.每个测验,我的应用程序将从问题收集中选择6个随机问题.

是否有可能在SailsJS 0.11,MongoDB 3.6.8?我怎样才能做到这一点?

非常感谢

Eug*_*kov 6

您可以find使用skiplimit标准调用方法.

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);
Run Code Online (Sandbox Code Playgroud)