GoLang MongoCursor::timeout

request it (279)
GoLang replacement for PHP's MongoCursor::timeout [edit | history]

Do you know a GoLang replacement for PHP's MongoCursor::timeout? Write it!

PHP MongoCursor::timeout

PHP original manual for MongoCursor::timeout [ show | php.net ]


(PECL mongo >=1.0.3)

MongoCursor::timeoutSets 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.



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).

Return Values

This cursor.


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


Example #1 MongoCursor::timeout() example

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



/* $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()) {

/* 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 {
    } catch (
MongoCursorTimeoutException $e) {
"query took too long!";




This does not cause the MongoDB server to cancel long-running operations; it only instructs the driver to stop waiting for a response and throw a MongoCursorTimeoutException after a set time. If you need to specify a server-side timeout for a query, consider using MongoCursor::maxTimeMS().

See Also