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:
Primary: read only from the primary. This is the default choice;
PrimaryPreferred: read from the primary if it is available, or secondaries if it is not;
Secondary: read only from any secondary;
SecondaryPreferred: read from any secondary, or from the primary if they are not available;
Nearest: read from the faster node (e.g. the node which replies faster than all others), regardless its status (primary or secondary.)
The default read preference can also be set in the connection options.
Tag sets allow you to specify custom read preferences and write concerns so that your application can target operations to specific members.
Custom read preferences and write concerns evaluate tags 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.
If you properly tagged the servers of your replica set, then you can use Tag-aware Read Preferences.
Let’s suppose that the replica set is configured that way:
Then we can tell ReactiveMongo to query only from the nodes that are tagged with