Random record from MongoDB

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

    it is tough if there is no data there to key off of. what are the _id field? are they mongodb object id's? If so, you could get the highest and lowest values:

    lowest = db.coll.find().sort({_id:1}).limit(1).next()._id;
    highest = db.coll.find().sort({_id:-1}).limit(1).next()._id;
    

    then if you assume the id's are uniformly distributed (but they aren't, but at least it's a start):

    unsigned long long L = first_8_bytes_of(lowest)
    unsigned long long H = first_8_bytes_of(highest)
    
    V = (H - L) * random_from_0_to_1();
    N = L + V;
    oid = N concat random_4_bytes();
    
    randomobj = db.coll.find({_id:{$gte:oid}}).limit(1);
    

提交回复
热议问题