Reactive Scala Driver for MongoDB
Asynchronous & Non-Blocking
The first thing you need to do is to create a new
Without any parameter,
MongoDriver creates a new Akka
ActorSystem. Obviously, you may want to reuse an existing system to avoid wasting resources.
Then you can connect to a MongoDB server.
Getting a database and a collection is pretty easy:
Connecting to a replica set
ReactiveMongo provides support for Replica Sets. That means the following:
- the driver will detect if it is connected to a Replica Set;
- it will probe for the other nodes in the set and connect to them;
- it will detect when the primary has changed and guess which is the new one;
- it will allow running queries on secondaries if they are explicitly set to
slaveOk(See the MongoDB documentation for more details about querying secondary nodes).
Connecting to a Replica Set is pretty much the same as connecting to a unique server. You may have notice that the argument to
driver.connection() method is a
List[String]; you can also give more than one node in the replica set.
There is no obligation to give all the nodes in the replica set – actually, just one of them is required. ReactiveMongo will ask the nodes it can reach for the addresses of the other nodes in the replica set. Obviously it is better to give at least 2 or more nodes, in case of unavailability of one node at the start of the application.
In some (rare) cases it is perfectly viable to create as many
MongoConnection instances you need with one
MongoDriver instance – in that case, you will get different connection pools. This is useful when your application has to connect to two or more independent MongoDB nodes (i.e. that do not belong to the same ReplicaSet), or different Replica Sets.
There are two ways to give ReactiveMongo your credentials.
- Using the
Like any other operation in ReactiveMongo, authentication is done asynchronously. Anyway, it is not mandatory to wait for the authentication result; thanks to the Failover Strategy, a request can be retried many times until the authentication process is done.
Connect Using MongoDB URI
You can also give the connection information as a URI:
If credentials and the database name are included in the URI, ReactiveMongo will authenticate the connections on that database.
MongoConnection stands for a pool of connections
Do not get confused here. A
MongoConnection is a logical connection, not a physical one; it is actually a connection pool. By default, a
MongoConnection creates 10 physical connections to each node in the replica set (or to the single node if it is not a replica set.) You can tune this by setting the
They manage two different things.
MongoDriver holds the actor system, and
MongoConnection the references to the actors. This is useful because it enables to work with many different single nodes or replica sets. Thus, your application can communicate is many different replica sets or single nodes, with only one
MongoConnection involve creation costs – the driver may create a new
ActorSystem, and the connection, well, will connect to the servers. It is also a good idea to store the driver and the connection to reuse them.
On the contrary,
collection are just plain objects that store references and nothing else. It is virtually free to create new instances; calling
db.collection() may be done many times without any performance hit.