开发者

Cursor Timed out error on MongoDb cursor

开发者 https://www.devze.com 2023-01-13 03:43 出处:网络
I have just started using mongoDb as my backend for PHP. I am simply using find() query for one of my needs. I want only first 100 results but also want to get total available results. I am trying th

I have just started using mongoDb as my backend for PHP.

I am simply using find() query for one of my needs. I want only first 100 results but also want to get total available results. I am trying this.

    $cursor = $this->dbReference->dbName->find($query);
    if($count != 0)
    {
            $cursor->skip($startIndex);
            $cursor->limit($count);
    }
    $totalCount = $cursor->count();

    $entries = array();
    while ($cursor->hasNext())
    {
            $cursor->next();
            $entry = $cursor->current();
            array_push($entries , $entry);
    }

Now The problem is.. T his search result contains exactly more than 50K results. But I am retrieving only 100 at a time. I am using $cursor->count() for getting total number of available result rows. on this line error is showing that "Cursor timed out". Please can anyone suggest me whats the problem? or what is the alternative to find total count of search result.

开发者_运维知识库

Thanks in advance.


You can solve cursor time out problem by adding this code before find():

MongoCursor::$timeout = -1;
$cursor = $this->dbReference->dbName->find($query);


I have just tried this out with 100,000 simple documents. $totalCount for me is always 100000, regardless of whether $count and $startIndex are set (this is the correct behaviour). $entries contains all 100000 entries. The whole operation takes about 3 seconds on my local setup.

Are you using a remote database? It's possible the network is what is causing the timeout rather than MongoDB.

What size are your documents? The volume of data can affect the speed.


I discovered that the ->count() also blows out the execution time until it times out. What is better to me is to just use find() and then place the desired item in the cursor into an array using a foreach loop. After that do an array_count_values() on that array. Seems to be a bit faster too.

Thanks for the MongoCursor::$timeout = -1 for I think it really helped in my situation too.

Magically there are no more horrible timeout messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号