Random record from MongoDB

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

    non of the solutions worked well for me. especially when there are many gaps and set is small. this worked very well for me(in php):

    $count = $collection->count($search);
    $skip = mt_rand(0, $count - 1);
    $result = $collection->find($search)->skip($skip)->limit(1)->getNext();
    

提交回复
热议问题