IMPORTANT: ReplicaSet support is not included in current V5 driver!
The driver described above is enough in a MongoDB standalone server configuration where there only one server can execute the operations. This job can get much more complex when the configuration is a MongoDB Replica Set. In this case, a group of servers maintain the same data set providing redundancy and high availability access.
The following figure shows a Replica Set configuration composed by 3 servers (a.k.a. members). The Primary server is the only member in the replica set that receives write operations. However, all members of the replica set can accept read operations (see Read Preference).
The replica set can have at most one primary. If the current primary becomes unavailable, an election determines the new primary.
To help in this kind of scenarios, the MongoClient
monitors the Replica Set status to provide the instance of Mongo
that your application requires to perform an operation.
You can create a client and make it start monitoring as follows:
client := MongoClient withUrls: urlsOfSomeReplicaSetMembers.
client start.
After some milliseconds, it should be ready to, for example, receive write operations such as:
client primaryMongoDo: [ :mongo |
((mongo
databaseNamed: 'test')
getCollection: 'pilots')
add: { 'name' -> 'Fangio' } asDictionary ].
Until more documentation is available, you have these options to learn about this client:
-
Example. Evaluate and browse this code:
MongoClientExample openInWindows
. -
Test suites. Browse the class hierarchy of
MongoClientTest
where you can see diverse tests, setUps, and tearDowns. -
Visual Monitor. You can check this repository, which watches the events announced by a
MongoClient
to help to better understand them via visualizations.