Random record from MongoDB

后端 未结 27 1924
栀梦
栀梦 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:40

    I'd suggest adding a random int field to each object. Then you can just do a

    findOne({random_field: {$gte: rand()}}) 
    

    to pick a random document. Just make sure you ensureIndex({random_field:1})

提交回复
热议问题