Random record from MongoDB

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

    My solution on php:

    /**
     * Get random docs from Mongo
     * @param $collection
     * @param $where
     * @param $fields
     * @param $limit
     * @author happy-code
     * @url happy-code.com
     */
    private function _mongodb_get_random (MongoCollection $collection, $where = array(), $fields = array(), $limit = false) {
    
        // Total docs
        $count = $collection->find($where, $fields)->count();
    
        if (!$limit) {
            // Get all docs
            $limit = $count;
        }
    
        $data = array();
        for( $i = 0; $i < $limit; $i++ ) {
    
            // Skip documents
            $skip = rand(0, ($count-1) );
            if ($skip !== 0) {
                $doc = $collection->find($where, $fields)->skip($skip)->limit(1)->getNext();
            } else {
                $doc = $collection->find($where, $fields)->limit(1)->getNext();
            }
    
            if (is_array($doc)) {
                // Catch document
                $data[ $doc['_id']->{'$id'} ] = $doc;
                // Ignore current document when making the next iteration
                $where['_id']['$nin'][] = $doc['_id'];
            }
    
            // Every iteration catch document and decrease in the total number of document
            $count--;
    
        }
    
        return $data;
    }
    

提交回复
热议问题