Random record from MongoDB

后端 未结 27 2460
栀梦
栀梦 2020-11-22 01:22

I am looking to get a random record from a huge (100 million record) mongodb.

What is the fastest and most efficient way to do so? The data is already t

27条回答
  •  独厮守ぢ
    2020-11-22 01:33

    If you have a simple id key, you could store all the id's in an array, and then pick a random id. (Ruby answer):

    ids = @coll.find({},fields:{_id:1}).to_a
    @coll.find(ids.sample).first
    

提交回复
热议问题