Database Reference
In-Depth Information
string regardless; this way, you remove the risk associated with trying to discover from only one member which may
be offline. The following example shows how to connect to a replica set from a PHP application:
<?php
$m = new MongoClient("mongodb://localhost:27021,
localhost:27022", array("replicaSet" => "testSet"));
...
?>
Setting Read Preference from within Your Application
A read preference in MongoDB is a way to select which members of a replica set you wish to read from. By specifying
a read preference to your driver you tell it to run queries against a particular member (or members) of a replica set.
Currently there are five modes that you can set as read preference on your driver, as listed in Table 11-6 .
Table 11-6. Read Preference Options
Option
Description
Primary
Reads will only be directed at the primary. This read preference is blocked if used explicitly
in conjunction with tagged read preferences. This is also the default read preference.
PrimaryPreferred
Reads will be directed at the primary, unless there is no primary available; then reads will
be directed at a secondary.
Secondary
Reads will only be directed at secondary nodes. If no secondary is available, this option will
generate an exception.
SecondaryPreferred
Reads will be directed at a secondary unless none is available; then reads will be directed at
a primary. This corresponds to the behavior of the old “slaveOk” secondary read method.
Nearest
Reads from the nearest node regardless of whether that is a primary or secondary. Nearest
uses network latency to determine which node to use.
If you set a read preference that means your reads may come from a secondary, you must be aware that this
data may not be fully up to date; certain operations may not have been replicated from your primary.
Note
You can set a read preference in PHP using the setReadPreference() command on a connection object
as follows:
<?php
$m = new MongoClient("mongodb://localhost:27021,
localhost:27022", array("replicaSet" => "testSet"));
$m->setReadPreference(MongoClient::RP_SECONDARY_PREFERRED, array());
...
?>
 
 
Search WWH ::




Custom Search