Database Reference
In-Depth Information
And from now, any queries you make on that connection will be run against secondary nodes in your cluster.
You can also set read preference by adding a read preference tag to your URI. A URI with a read preference of nearest
specified would look like this:
mongodb://localhost:27021,localhost:27022?readPreference=nearest
Setting Write Concern from within Your Application
Write concern is a similar concept to read preference. You use write concern to specify how many nodes this data
needs to have been safely committed to before it is considered to be “complete. “This test is done using MongoDB's
Get Last Error (GLE) mechanism to check the last error that occurred on the connection. You can set several write
concern modes, which allow you to configure how certain you are that a write will be persisted when it is executed.
Each is listed in Table 11-7 .
Table 11-7. MongoDB Write Concern Levels
Option
Description
W=0 or Unacknowledged
A fire-and-forget write. The write will be sent, but no attempt to acknowledge
if it was committed will be made.
W=1 or Acknowledged
A write must be confirmed by the primary. This is the default.
W=N or Replica Set
Acknowledged
The primary must confirm the write, and N -1 members must replicate this
write from the primary. This option is more robust but can cause delays
if there is replication lag on members in your replica set, or if not enough
members are up at the time the write is committed because of an outage or
the like.
W=Majority
A write must be written to the primary and replicated by enough members
that a majority of members in the set have confirmed the write. As with w=n ,
this can cause problems during outages or if there is replication lag.
J=true or Journaled
Can be used with w= write concerns to specify that the write must be persisted
to the journal to be considered confirmed.
In order to use a write concern with an insert, you simply add the w option to the given insert() function, as
shown here:
$col->insert($ document , array ("w" => 1));
This attempts to insert a document into our collection with a w=1 value for an acknowledged write.
Using Tags for Read Preference and Write Concern
In addition to the read preference and write concern options just discussed, there is another way you can proceed—tags.
This mechanism allows you to set custom tags on members of your replica sets and then use these tags with your read
preference and write concern settings to direct operations in a more granular fashion. So, without further ado, let's get
 
 
Search WWH ::




Custom Search