node.js - 风 sails JS : Select random records from MongoDB?

标签 node.js mongodb sails.js

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

SailsJS 0.11、MongoDB 3.6.8 可以吗? 我该怎么做?

非常感谢

最佳答案

您可以使用skiplimit 条件调用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/

相关文章:

node.js - 如何制作和存储帖子标题的子弹?

javascript - 由 C++ 中的 opencv 控制的 Parrot AR 无人机

php - 寻找适合我们案例的 NoSQL 数据库

javascript - 如何使用 Wolfpack.js 测试 Sails.js 模型的独特电子邮件属性?

angularjs - Angular 中的 JWT 身份验证 - 多个身份验证级别

javascript - 使用 Loopback 实现更改密码

node.js - 是否可以从字符串生成 RSA key ?

javascript - 如何 react 性地渲染你自己的对象实例? (不是游标对象)

MongoDB Aggregate $unwind $match using date - 我错过了什么?

sql - Waterline ORM 相当于在重复键更新时插入