Docs Menu
Docs Home
/
MongoDB Manual
/ / /

cursor.addOption()

On this page

  • Definition
  • Flags
  • Example
cursor.addOption(flag)

Important

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.

Used to change query behavior by setting the flags listed below.

The cursor.addOption() method has the following parameter:

Parameter
Type
Description
flag
flag
OP_QUERY wire protocol flag. For the mongo shell, you can use the cursor flags listed below. For the driver-specific list, see your driver documentation.

The mongo shell provides several additional cursor flags to modify the behavior of the cursor.

Flag
Description
DBQuery.Option.tailable
Sets the cursor not to close once the last data is received, allowing the query to continue returning data added after the initial results were exhausted.
DBQuery.Option.slaveOk
Allows querying of a replica slave.
DBQuery.Option.noTimeout
Prevents the server from timing out idle cursors.
DBQuery.Option.awaitData
For use with DBQuery.Option.tailable. Sets the cursor to block the query thread when no data is available and await data for a set time instead of immediately returning no data. The cursor returns no data only if the timeout expires.
DBQuery.Option.exhaust
Sets the cursor to return all data returned by the query at once rather than splitting the results into batches.
DBQuery.Option.partial
Sets the cursor to return partial data from a query against a sharded cluster in which some shards do not respond rather than throwing an error.

The following example adds the DBQuery.Option.tailable flag and the DBQuery.Option.awaitData flag to ensure that the query returns a tailable cursor. The sequence creates a cursor. After returning the full result set, it waits for the default interval of 1000 milliseconds so that it can capture and return additional data added during the query:

var t = db.myCappedCollection;
var cursor = t.find().addOption(DBQuery.Option.tailable).
addOption(DBQuery.Option.awaitData)

Warning

Adding incorrect wire protocol flags can cause problems and/or extra server load.

Back

Cursor Methods