Skip to content

Commit ea9a7ce

Browse files
committed
Adding cursor timeout, fixes #252
1 parent ea5299d commit ea9a7ce

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,12 @@ The MongoClient and MongoDB objects can be accessed like this:
557557

558558
### MongoDB specific operations
559559

560+
**Cursor timeout**
561+
562+
To prevent MongoCursorTimeout exceptions, you can manually set a timeout value that will be applied to the cursor:
563+
564+
DB::collection('users')->timeout(-1)->get();
565+
560566
**Upsert**
561567

562568
Update or insert a document. Additional options for the update method are passed directly to the native update method.

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
2525
*/
2626
public $projections;
2727

28+
/**
29+
* The cursor timeout value.
30+
*
31+
* @var int
32+
*/
33+
public $timeout;
34+
2835
/**
2936
* All of the available clause operators.
3037
*
@@ -79,6 +86,19 @@ public function project($columns)
7986
return $this;
8087
}
8188

89+
/**
90+
* Set the cursor timeout in seconds.
91+
*
92+
* @param int $seconds
93+
* @return $this
94+
*/
95+
public function timeout($seconds)
96+
{
97+
$this->timeout = $seconds;
98+
99+
return $this;
100+
}
101+
82102
/**
83103
* Execute a query for a single record by ID.
84104
*
@@ -217,9 +237,10 @@ public function getFresh($columns = array())
217237
$cursor = $this->collection->find($wheres, $columns);
218238

219239
// Apply order, offset and limit
220-
if ($this->orders) $cursor->sort($this->orders);
221-
if ($this->offset) $cursor->skip($this->offset);
222-
if ($this->limit) $cursor->limit($this->limit);
240+
if ($this->timeout) $cursor->timeout($this->timeout);
241+
if ($this->orders) $cursor->sort($this->orders);
242+
if ($this->offset) $cursor->skip($this->offset);
243+
if ($this->limit) $cursor->limit($this->limit);
223244

224245
// Return results as an array with numeric keys
225246
return iterator_to_array($cursor, false);

0 commit comments

Comments
 (0)