Mongoid random document

前端 未结 9 1413
迷失自我
迷失自我 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:27

    If you really want simplicity you could use this instead:

    class Mongoid::Criteria
    
      def random(n = 1)
        indexes = (0..self.count-1).sort_by{rand}.slice(0,n).collect!
    
        if n == 1
          return self.skip(indexes.first).first
        else
          return indexes.map{ |index| self.skip(index).first }
        end
      end
    
    end
    
    module Mongoid
      module Finders
    
        def random(n = 1)
          criteria.random(n)
        end
    
      end
    end
    

    You just have to call User.random(5) and you'll get 5 random users. It'll also work with filtering, so if you want only registered users you can do User.where(:registered => true).random(5).

    This will take a while for large collections so I recommend using an alternate method where you would take a random division of the count (e.g.: 25 000 to 30 000) and randomize that range.

提交回复
热议问题