Cursor error on MongoDb cursor

I just started using mongoDb as my backend for PHP.

I just use the find () query for one of my needs. I want only the first 100 results, but also want to get the final results. I am trying to do 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 that T its search result contains exactly more than 50 thousand results. But I only get 100 at a time. I use $ cursor-> count () to get the total number of result lines available. on this line the error indicates that "Cursor timed out". Please can anyone suggest me what is the problem? or what is an alternative to search for the total number of search results.

Thanks in advance.

+4
source share
3 answers

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

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

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

Are you using a remote database? It is possible that the network is causing the timeout, not MongoDB.

What is the size of your documents? The amount of data can affect speed.

+1
source

I found that → count () also gives a runtime until it expires. For me, it's best to use find () and then put the item in the cursor into an array using the foreach loop. After that, execute array_count_values ​​() in this array. It seems to be a little faster too.

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

Magically, there are no more terrible timeout messages.

0
source

All Articles