Reactive Scala Driver for MongoDB

Asynchronous & Non-Blocking

Use Read Preferences

Read preference describes how MongoDB clients route read operations to members of a replica set. (MongoDB Read Preference Documentation)

The following read preferences are supported:

The Read preference can be chosen globally using the MongoConnectionOptions, or for each cursor.

import scala.concurrent.ExecutionContext.Implicits.global

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.{ Cursor, ReadPreference }
import reactivemongo.api.bson.collection.BSONCollection

def readFromSecondary1(collection: BSONCollection) = 
  collection.find(BSONDocument("city" -> "San Francisco")).
    // read from any secondary whenever possible
    cursor[BSONDocument](ReadPreference.secondaryPreferred).
    collect[List](-1, Cursor.FailOnError[List[BSONDocument]]())

The default read preference can be set in the connection options.

Tag support

Tag sets allow you to specify custom read preferences and write concerns so that your application can dispatch the operations to specific members.

With tagged servers in your replica set, then you can use a tag-aware Read Preferences.

Let’s suppose that the replica set is configured that way:

{
    "_id" : "rs0",
    "version" : 2,
    "members" : [
             {
                     "_id" : 0,
                     "host" : "mongodb0.example.net:27017",
                     "tags" : {
                             "dc": "NYC",
                             "disk": "ssd"
                     }
             },
             {
                     "_id" : 1,
                     "host" : "mongodb1.example.net:27017",
                     "tags" : {
                             "dc": "NYC"
                     }
             },
             {
                     "_id" : 2,
                     "host" : "mongodb2.example.net:27017",
                     "tags" : {
                             "dc": "Paris"
                     }
             }
     ]
}

Then we can tell ReactiveMongo to query only from the nodes which are tagged with dc: "NYC":

import scala.concurrent.ExecutionContext.Implicits.global

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.ReadPreference
import reactivemongo.api.bson.collection.BSONCollection

def readFromSecondary2(collection: BSONCollection) = 
  collection.find(BSONDocument("city" -> "San Francisco")).
    // read from any secondary tagged with `dc: "NYC"`
    one[BSONDocument](ReadPreference.secondaryPreferred)

The custom read preferences and write concerns evaluate the tag sets in different ways. Read preferences consider the value of a tag when selecting a member to read from. Write concerns ignore the value of a tag to when selecting a member, except to consider whether or not the value is unique; See MongoDB Read Preference Documentation)

Suggest changes