OPTIONS

cursor.addOption()

Definition

cursor.addOption(flag)

Adds OP_QUERY wire protocol flags, such as the tailable flag, to change the behavior of queries.

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

Parameter Type Description
flag flag OP_QUERY wire protocol flag. See MongoDB wire protocol for more information on MongoDB Wire Protocols and the OP_QUERY flags. For the mongo shell, you can use cursor flags. For the driver-specific list, see your driver documentation.

Flags

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

DBQuery.Option.tailable
DBQuery.Option.slaveOk
DBQuery.Option.oplogReplay
DBQuery.Option.noTimeout
DBQuery.Option.awaitData
DBQuery.Option.exhaust
DBQuery.Option.partial

For a description of the flags, see MongoDB wire protocol.

Example

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 that will wait for few seconds after returning the full result set 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.