Mongoid random document

前端 未结 9 1398
迷失自我
迷失自我 2021-02-06 00:49

Lets say I have a Collection of users. Is there a way of using mongoid to find n random users in the collection where it does not return the same user twice? For now lets say

9条回答
  •  清歌不尽
    2021-02-06 01:19

    I think it is better to focus on randomizing the returned result set so I tried:

    Model.all.to_a.shuffle
    

    Hope this helps.

提交回复
热议问题