我是 Sails JS 框架的新手,我正在尝试编写一个小测验应用程序。每次测验,我的应用程序都会从问题集合中随机选择 6 个问题。
SailsJS 0.11、MongoDB 3.6.8 可以吗? 我该怎么做?
非常感谢
最佳答案
您可以使用skip
和limit
条件调用find
方法。
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);
关于node.js - 风 sails JS : Select random records from MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33122521/