Manual
PHP Manual

Read Preferences

MongoDB 2.2 and version 1.3.0 of the driver add support for » read preferences, which allow control over how queries are directed to mongod instances. Read preferences may be specified on either a per-connection, per-database, per-collection, or per-cursor basis. Preferences defined at a higher level will be inherited by default (e.g. MongoCollection will inherit read perferences defined on the corresponding MongoDB instance).

Read preferences are specified with a combination of modes and tag sets. Modes determine how mongod instances are prioritized, while » tag sets specify criteria for eligible mongod instances.

Read Preference Modes

Warning

All read preference modes except Mongo::RP_PRIMARY may return stale data as secondaries replicate operations from the primary with some delay. Ensure that your application can tolerate stale data if you choose to use a mode other than Mongo::RP_PRIMARY.

Tag Sets

» Tag sets allow you to specify criteria so that your application can target read operations to specific members, based on custom parameters. Tag sets make it possible to ensure that read operations target members in a particular data center or target mongod instances designated for a particular class of operations, such as reporting or analytics.

You can specify tag sets with the following read preference modes:

You cannot specify tag sets with the Mongo::RP_PRIMARY read preference mode. Tags apply only when selecting a secondary member of a set, except for the when in the nearest mode.

Specifying Read Preferences

Read preferences may be specified in either the connection URI provided to Mongo::__construct(), which uses a query string syntax, or via setter methods on the core classes, which use an array syntax for tag sets.

When specifying read preference modes in a query string, the names primary, primaryPreferred, secondary, secondaryPreferred, and nearest should be used instead of the constants for the readPreference value. Tag sets for the readPreferenceTags value should be a comma-delimited sequence of colon-delimited key/value pairs.

Note:

Each tag set defined in the query string will use the readPreferenceTags name. Unlike how PHP might handle URL query strings, successive values for readPreferenceTags will not overwrite each other. The driver will collect tag sets in the order they appear in the query string.

Example #1 Connection URI read preferences with query string syntax

<?php

// Prefer the nearest server with no tag preference
$uri  'mongodb://rs1.example.com,rs2.example.com/';
$uri .= '?readPreference=nearest';
$m = new Mongo($uri, array('replicaSet' => 'rs'));

// Prefer the nearest server in the "east" data center
$uri  'mongodb://rs1.example.com,rs2.example.com/';
$uri .= '?readPreference=nearest';
$uri .= '&readPreferenceTags=dc:east';
$m = new Mongo($uri, array('replicaSet' => 'rs'));

// Prefer the nearest server in the "east" data center also used for reporting,
// but fall back to a server in the "west" data center
$uri  'mongodb://rs1.example.com,rs2.example.com/';
$uri .= '?readPreference=nearest';
$uri .= '&readPreferenceTags=dc:east,use:reporting';
$uri .= '&readPreferenceTags=dc:west';
$m = new Mongo($uri, array('replicaSet' => 'rs'));

// Prefer the nearest server in the "east" data center, then a server in the
// "west" data center, and finally fall back to no tag set preference
$uri  'mongodb://rs1.example.com,rs2.example.com/';
$uri .= '?readPreference=nearest';
$uri .= '&readPreferenceTags=dc:east';
$uri .= '&readPreferenceTags=dc:west';
$uri .= '&readPreferenceTags=';
$m = new Mongo($uri, array('replicaSet' => 'rs'));

Example #2 Setting read preferences with array syntax for tag sets

<?php

$m 
= new Mongo('mongodb://rs1.example.com,rs2.example.com', array(
    
'replicaSet' => 'rs',
));

// Prefer the nearest server with no tag preference
$m->setReadPreference(Mongo::RP_NEAREST, array());

// Prefer the nearest server in the "east" data center
$m->setReadPreference(Mongo::RP_NEAREST, array(
    array(
'dc' => 'east'),
));

// Prefer the nearest server in the "east" data center also used for reporting,
// but fall back to a server in the "west" data center
$m->setReadPreference(Mongo::RP_NEAREST, array(
    array(
'dc' => 'east''use' => 'reporting'),
    array(
'dc' => 'west'),
));

// Prefer the nearest server in the "east" data center, then a server in the
// "west" data center, and finally fall back to no tag set preference
$m->setReadPreference(Mongo::RP_NEAREST, array(
    array(
'dc' => 'east'),
    array(
'dc' => 'west'),
    array(),
));


Manual
PHP Manual