Connect to MongoDB

Use MongoClient() to make a connection to a running MongoDB instance.


The following examples are not meant to provide an exhaustive list of ways to instantiate MongoClient. For a complete list of MongoClient companion methods, see the MongoClient API documentation.


It is strongly recommended that system keep-alive settings should be configured with shorter timeouts.

See the ‘does TCP keep-alive time affect MongoDB deployments?’ documentation for more information.


  • Running MongoDB deployments to which to connect. For example, to connect to a standalone, you must have a running standalone.

  • The MongoDB Driver. See Installation for instructions on how to install the MongoDB driver.

  • The following import statements:

import org.mongodb.scala._

import scala.collection.JavaConverters._


A MongoClient instance represents a pool of connections to the database; you will only need one instance of class MongoClient even with multiple concurrent operations.


Typically you only create one MongoClient instance for a given MongoDB deployment (e.g. standalone, replica set, or a sharded cluster) and use it across your application. However, if you do create multiple instances:

  • All resource usage limits (e.g. max connections, etc.) apply per MongoClient instance.

  • To dispose of an instance, call MongoClient.close() to clean up resources.

Connect to a Standalone MongoDB Instance

To connect to a standalone MongoDB instance:

  • You can create a MongoClient object without any parameters to connect to a MongoDB instance running on localhost on port 27017:

    val mongoClient = MongoClient()
  • You can explicitly specify the hostname to connect to a MongoDB instance running on the specified host on port 27017:

    val mongoClient = MongoClient("mongodb://host1")
  • You can explicitly specify the hostname and the port:

    val mongoClient = MongoClient("mongodb://host1:27017")

Connect to a Replica Set

To connect to a replica set, you must specify one or more members to the MongoClient apply method.


MongoDB will auto-discover the primary and the secondaries.

  • You can specify the members using a ConnectionString:

    • To specify at least two members of the replica set:
val mongoClient = MongoClient("mongodb://host1:27017,host2:27017,host3:27017")
  • With at least one member of the replica set and the replicaSet option specifying the replica set name:
val mongoClient = MongoClient("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet")
  • You can specify a list of the all the replica set members’ ServerAddress:
val mongoClient = MongoClient(
    .applyToClusterSettings((builder: ClusterSettings.Builder) => builder.hosts(List(
        new ServerAddress("host1", 27017),
        new ServerAddress("host2", 27017),
        new ServerAddress("host3", 27017)).asJava))

Connect to a Sharded Cluster

To connect to a sharded cluster, specify the mongos instance or instances to a MongoClient apply method.

To connect to a single mongos instance:

val mongoClient = MongoClient( "mongodb://localhost:27017" )

or leave the connection string out if the mongos is running on localhost:27017:

val mongoClient = MongoClient()

To connect to multiple mongos instances:

  • You can specify the ConnectionString with their hostnames and ports:

    val mongoClient = MongoClient("mongodb://host1:27017,host2:27017")
  • You can specify a list of the mongos instances’ ServerAddress:

val mongoClient = MongoClient(
    .applyToClusterSettings((builder: ClusterSettings.Builder) => builder.hosts(List(
        new ServerAddress("host1", 27017),
        new ServerAddress("host2", 27017)).asJava))

Connection Options

You can specify the connection settings using either the ConnectionString or MongoClientSettings or both.

For example, you can specify TLS/SSL and authentication setting in the connection string:

val mongoClient = MongoClient("mongodb://user1:pwd1@host1/?authSource=db1&ssl=true")

You can also use MongoClientSettings to specify TLS/SSL and the MongoCredential for the authentication information:

val user: String = ???          // the user name
val source: String = ???        // the source where the user is defined
val password: Array[Char] = ??? // the password as a character array
// ...
val credential = MongoCredential.createCredential(user, source, password)

val mongoClient: MongoClient = MongoClient(
    .applyToSslSettings((builder: SslSettings.Builder) => builder.enabled(true))
    .applyToClusterSettings((builder: ClusterSettings.Builder) => builder.hosts(List(new ServerAddress("host1", 27017)).asJava))

Finally, in some cases you may need to combine a connection string with programmatic configuration:

val connectionString = ConnectionString("mongodb://host1:27107,host2:27017/?ssl=true")
val myCommandListener: CommandListener = ???

val mongoClient = MongoClient(