cursor.addOption()

Definition

  • cursor.addOption(flag)

mongo Shell Method

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

Deprecated since v3.2

Starting in v3.2, the cursor.addOption() operator is deprecated in themongo shell. In the mongo shell,use available cursor methods instead.

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

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

ParameterTypeDescriptionflagflagOP_QUERY wire protocol flag. For the mongo shell,you can use the cursor flags listed below. For the driver-specificlist, see your driver documentation.

Flags

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

FlagDescription
- DBQuery.Option.tailable-Sets the cursor not to close once the last data isreceived, allowing the query to continue returning data addedafter 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 .. data:: DBQuery.Option.tailable; sets the cursorto block and await data for a while rather than returning nodata. The cursor will return no data once the timeout hasexpired.
- DBQuery.Option.exhaust-Sets the cursor to return all data returned by thequery at once rather than splitting the results into batches.
- DBQuery.Option.partial-Sets the cursor to return partial data from a query against asharded cluster in which some shards do not respond rather thanthrowing an error.

Example

The following example adds the DBQuery.Option.tailable flag and theDBQuery.Option.awaitData flag to ensure that the query returns atailable cursor. The sequence creates a cursor that will wait for fewseconds after returning the full result set so that it can capture andreturn additional data added during the query:

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

Warning

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