从数据库中随机淡入和淡出数组元素
我正在开发一个个人项目,用户提交一份声明并将其添加到 MongoDB 集合中。假设该集合共有 50 个语句。我希望这些语句能够随机淡入和淡出,并且在任何给定时间网页上最多只有 7 个语句。
我不确定如何解决这个问题,因为理想情况下我想利用集合中的所有语句。我正在考虑创建一个数组并随机选择 7 个语句来填充这个数组。这就是我所得到的,因为那我该怎么办?是否与我的数据库保持持续通信以随机替换数组中的元素?不确定这是否可以做到/是否可取。或者我应该预先将集合加载到页面中?不确定是否建议这样做,因为如果我们谈论的是一个包含大量条目的集合怎么办?
或者也许我的两种方法都是错误的。希望有人可以分享一些伪代码的方法。
I am working on a personal project where users submit a statement and it is added to a MongoDB collection. Assume this collection has a total of 50 statements. I would like these statements to fade in and out at random with there only being a maximum of 7 statements on the webpage at any given time.
I'm not sure how to approach this problem because ideally I'd want to make use of all the statements in the collection. I was thinking of creating an array and randomly selecting 7 statements to fill this array. That is as far as I've gotten because then what shall I do? Be in constant communication with my database to randomly replace elements in the array? Not sure if that can be done/is advisable. Or should I have the collection loaded into the page beforehand? Not sure if that's recommended either because what if we're talking about a collection with an extremely large amount of entries.
Or maybe both of my approaches are wrong. Hope someone can share an approach with some pseudocode perhaps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
$sample
mongoplayground
Use
$sample
mongoplayground