The Scala driver has two ways of specifying the settings of a connection to a MongoDB server deployment.
The connection string is the simplest way to specify the properties of a connection. . A connection string mostly follows RFC 3986, with the exception of the domain name. For MongoDB, it is possible to list multiple domain names separated by a comma. Below are some example connection strings.
- For a standalone mongod, mongos, or a direct connection to a member of a replica set:
- To connect to multiple mongos or a replica set:
The authentication guide contains information on how to provide credentials in the connection string.
The Database Component
The database component is optional and is used to indicate which database to authenticate against. When the database component is not provided, the “admin” database is used.
Above, the database by the name of “mydb” is where the credentials are stored for the application.
Some drivers utilize the database component to indicate which database to work with by default. The Scala driver, while it parses the database component, does not use the database component for anything other than authentication.
Many options can be provided via the connection string. The ones that cannot may be provided in a
MongoClientSettings instance. To
provide an option, append a
? to the connection string and separate options by an
The above connection string sets the “replicaSet” value to “rs0” and the “maxPoolSize” to “200”.
For a comprehensive list of the available options, see the
MongoClient instance will be the root object for all interaction with MongoDB. It is all
that is needed to handle connecting to servers, monitoring servers, and performing operations against those servers.
val client: MongoClient = MongoClient()
Alternatively, a connection string may be provided:
val client: MongoClient = MongoClient("mongodb://host:27017,host2:27017/?replicaSet=rs0")
MongoClientSettings class provides an in-code way to set the
same options from a connection string. This is sometimes necessary, as the connection string does not allow an application to configure as
many properties of the connection as
MongoClientSettings instances are immutable, so to create one an
application uses a builder:
import scala.collection.JavaConverters._ import org.mongodb.scala.connection.ClusterSettings val clusterSettings: ClusterSettings = ClusterSettings.builder().hosts(List(new ServerAddress("localhost")).asJava).description("Local Server").build() val settings: MongoClientSettings = MongoClientSettings.builder().clusterSettings(clusterSettings).build() val client: MongoClient = MongoClient(settings)
By default, the async driver relies on the
AsynchronousSocketChannel class, introduced
in Java 7. If configured properly, the driver will use Netty instead. An application must use Netty for the
- The application is configured to use SSL to communicate with the MongoDB server.
- The application runs on a Java 6 JVM.
To configure the driver to use Netty, the application must configure the MongoClientSettings appropriately: