tags:

views:

33

answers:

1

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.

+1  A: 

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.

danielgwood
@danielwood Yes, I am using remote database , but it is also on local network(Intranet). Might be that problem will be because of $cursor->count(); And also $cursor->count() increases execution time. If I am not using $cursor->count() It is performing fast as u said.
Maulik Vora