gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:24:24 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com