开发者

what's the simplest and most effective way to get random records from a mongodb collection?

开发者 https://www.devze.com 2023-03-26 06:45 出处:网络
I\'m trying to get a list of users from my user collection, but need to get 20 from 10k+ randomly.What\'s t开发者_JS百科he cleanest and most effective way?

I'm trying to get a list of users from my user collection, but need to get 20 from 10k+ randomly. What's t开发者_JS百科he cleanest and most effective way?

I was thinking of updating users with {random: Math.random()} every minute and then sorting by random....

Is there a cleaner / more effective way of doing this?


It looks like your solution is pretty good : http://cookbook.mongodb.org/patterns/random-attribute/


You can get a list of element and just change the offset

User.all.offset(rand(User.count).limit(20)
0

精彩评论

暂无评论...
验证码 换一张
取 消