MongoCursor::timeout

(PECL mongo >=1.0.3)
Sets a client-side timeout for this query
public MongoCursor MongoCursor::timeout ( int $ms )

A timeout can be set at any time and will affect subsequent queries on the cursor, including fetching more results from the database.

Parameters:
ms

The number of milliseconds for the cursor to wait for a response. Use -1 to wait forever. By default, the cursor will wait 30000 milliseconds (30 seconds).

Returns:

This cursor.

Exception:

Causes methods that fetch results to throw a MongoCursorTimeoutException if the query takes longer than the specified number of milliseconds.

Examples:
MongoCursor::timeout() example

In the following example, the driver will wait forever for the initial database response, and then wait 100ms for subsequent responses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
$cursor $collection->find();
$cursor->timeout(-1);
 
/* $cursor->hasNext() executes the query. An infinite timeout has been set, so
 * the driver will wait as long as necessary for a response.
 */
while ($cursor->hasNext()) {
    $cursor->timeout(100);
 
    /* A timeout has now been set, so if the cursor needs to get more results
     * from the database, it will only wait 100ms for a response.
     */
    try {
        print_r($cursor->getNext());
    catch (MongoCursorTimeoutException $e) {
        echo "query took too long!";
    }
}
 
?>
See also:

MongoCursorInterface::timeout() -

MongoClient::__construct() -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.