Sha256: 8e21fc3862f0ef1229a26976b5a81a8c5026200c0488b2408f1b9f63c92a7497

Contents?: true

Size: 1.99 KB

Versions: 101

Compression:

Stored size: 1.99 KB

Contents

# Read Preference in Ruby

## Setting the read preference

You can using the `:read` option to specify a query's read preference. There are for now two possible options:

    @collection.find({:doc => 'foo'}, :read => :primary)
    @collection.find({:doc => 'foo'}, :read => :secondary)

In the first case, the query will be directed to the primary node in a replica set. In the second, the query will be sent
to a secondary node. The driver will attempt to choose a secondary node that's nearby, as determined by ping time. If more
than one secondary node is closeby (e.g, responds to pings within 10ms), then a random node within this subset will be chosen.

## Read preference inheritance

The Ruby driver allows you to set read preference on each of four levels: the connection, database, collection, and cursor (or read operation).
Objects will inherit the default read preference from their parents. Thus, if you set a read preference of `{:read => :secondary}` when creating
a new connection, then all databases and collections created from that connection will inherit the same setting. See this code example:

    @con = Mongo::ReplSetConnection.new(['localhost:27017','localhost:27018'], :read => :secondary)
    @db  = @con['test']
    @collection = @db['foo']
    @collection.find({:name => 'foo'})

    @collection.find({:name => 'bar'}, :read => :primary)

Here, the first call to Collection#find will use the inherited read preference, `{:read => :secondary}`. But the second call
to Collection#find overrides this setting by setting the preference to `:primary`.

You can examine the read preference on any object by calling its `read_preference` method:

    @con.read_preference
    @db.read_preference
    @collection.read_preference

## Future work

In the v2.0 release of the driver, you'll also be able to specify a read preference consisting of a set of tags. This way,
you'll be able to direct reads to a replica set member. You can follow this issue's progress here: (https://jira.mongodb.org/browse/RUBY-326).

Version data entries

101 entries across 55 versions & 3 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.7.5 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.7.4 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.7.4 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.7.3 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.7.3 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.7.2 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.7.2 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.7.1 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.7.1 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.7.0 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.7.0 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.6.9 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.6.9 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.6.8 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.6.8 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.6.7 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.6.7 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md
classiccms-0.6.6 vendor/bundle/gems/mongo-1.6.1/docs/READ_PREFERENCE.md
classiccms-0.6.6 vendor/bundle/gems/mongo-1.6.2/docs/READ_PREFERENCE.md