Class: ReadPreference

ReadPreference

new ReadPreference(mode, tags, options){ReadPreference}

The ReadPreference class is a class that represents a MongoDB ReadPreference and is
used to construct connections.

Name Type Description
mode string

A string describing the read preference mode (primary|primaryPreferred|secondary|secondaryPreferred|nearest)

tags array

The tags object

options object optional

Additional read preference options

Name Type Description
maxStalenessSeconds number optional

Max secondary read staleness in seconds, Minimum value is 90 seconds.

See:
Example

const ReplSet = require('mongodb-core').ReplSet,
ReadPreference = require('mongodb-core').ReadPreference,
assert = require('assert');

const server = new ReplSet([{host: 'localhost', port: 30000}], {setName: 'rs'});
// Wait for the connection event
server.on('connect', function(server) {
const cursor = server.cursor(
'db.test',
{ find: 'db.test', query: {} },
{ readPreference: new ReadPreference('secondary') }
);

cursor.next(function(err, doc) {
server.destroy();
});
});

// Start connecting
server.connect();

Members

staticReadPreference.nearestReadPreference

Nearest read preference

staticReadPreference.primaryReadPreference

Primary read preference

staticReadPreference.primaryPreferredReadPreference

Primary Preferred read preference

staticReadPreference.secondaryReadPreference

Secondary read preference

staticReadPreference.secondaryPreferredReadPreference

Secondary Preferred read preference

Methods

staticReadPreference.isValid(mode){boolean}

Validate if a mode is legal

Name Type Description
mode string

The string representing the read preference mode.

Returns:
if a mode is valid

equals(readPreference){boolean}

Are the two read preference equal

Name Type Description
readPreference ReadPreference

The read preference with which to check equality

Returns:
if the two ReadPreferences are equivalent

Validate if a mode is legal

Name Type Description
mode string

The string representing the read preference mode.

Returns:
if a mode is valid

Indicates that this readPreference needs the "slaveOk" bit when sent over the wire

See:

Return JSON representation

Returns:
JSON representation of the ReadPreference