Query.prototype.read()

Parameters
  • pref «String» one of the listed preference options or aliases

  • [tags] «Array» optional tags for this query

Returns:
  • «Query» this

Determines the MongoDB nodes from which to read.

Preferences:

  1. primary - (default) Read from primary only. Operations will produce an error if primary is unavailable. Cannot be combined with tags.
  2. secondary Read from secondary if available, otherwise error.
  3. primaryPreferred Read from primary if available, otherwise a secondary.
  4. secondaryPreferred Read from a secondary if available, otherwise read from the primary.
  5. nearest All operations read from among the nearest candidates, but unlike other modes, this option will include both the primary and all secondaries in the random selection.

Aliases

  1. p primary
  2. pp primaryPreferred
  3. s secondary
  4. sp secondaryPreferred
  5. n nearest

Example:

  1. new Query().read('primary')
  2. new Query().read('p') // same as primary
  3. new Query().read('primaryPreferred')
  4. new Query().read('pp') // same as primaryPreferred
  5. new Query().read('secondary')
  6. new Query().read('s') // same as secondary
  7. new Query().read('secondaryPreferred')
  8. new Query().read('sp') // same as secondaryPreferred
  9. new Query().read('nearest')
  10. new Query().read('n') // same as nearest
  11. // read from secondaries with matching tags
  12. new Query().read('s', [{ dc:'sf', s: 1 },{ dc:'ma', s: 2 }])

Read more about how to use read preferrences here and here.