开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜