Docs Menu
Docs Home
/ / /
Scala
/

Choose a Connection Target

On this page

  • Overview
  • Atlas
  • Local Deployments
  • Replica Sets
  • API Documentation

In this guide, you can learn how to use a connection string and a MongoClient object to connect to different types of MongoDB deployments.

To connect to a MongoDB deployment on Atlas, include the following elements in your connection string:

  • The URL of your Atlas cluster

  • Your MongoDB username

  • Your MongoDB password

Then, pass your connection string to the MongoClient constructor.

Tip

Follow the Atlas driver connection guide to retrieve your connection string.

When you connect to Atlas, we recommend using the Stable API client option to avoid breaking changes when Atlas upgrades to a new version of MongoDB Server. To learn more about the Stable API feature, see the Stable API guide.

The following code shows how to use the Scala driver to connect to an Atlas cluster. The code also uses the serverApi() method to specify a Stable API version.

object MongoClientConnectToAtlas {
def main(args: Array[String]): Unit = {
// Replace the placeholder with your Atlas connection string
val connectionString = "<connection string>";
// Constructs a ServerApi instance using the ServerApi.builder() method
val serverApi = ServerApi.builder.version(ServerApiVersion.V1).build()
val settings = MongoClientSettings
.builder()
.applyConnectionString(ConnectionString(connectionString))
.serverApi(serverApi)
.build()
// Creates a new client and connects to the server
Using(MongoClient(settings)) { mongoClient =>
// Sends a ping to confirm a successful connection
val database = mongoClient.getDatabase("admin")
val ping = database.runCommand(Document("ping" -> 1)).head()
Await.result(ping, 10.seconds)
System.out.println("Pinged your deployment. You successfully connected to MongoDB!")
}
}
}

You can connect to a local MongoDB deployment in the following ways:

  • Instantiate a MongoClient object without any parameters to connect to a MongoDB server running on localhost on port 27017:

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

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

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

To connect to a replica set, specify the hostnames (or IP addresses) and port numbers of the replica set members.

If you aren't able to provide a full list of hosts in the replica set, you can specify one or more of the hosts in the replica set and instruct the Scala driver to perform automatic discovery to find the others. To instruct the driver to perform automatic discovery, perform one of the following actions:

  • Specify the name of the replica set as the value of the replicaSet parameter.

  • Specify false as the value of the directConnection parameter.

  • Specify more than one host in the replica set.

You can connect to a MongoDB replica set by specifying the members in a ConnectionString. The following example shows how to specify three members of the replica set:

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

The following example shows how to specify members of the replica set and the replicaSet option with the replica set name:

val mongoClient = MongoClient("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet")

The following example shows how to specify a list of ServerAddress instances corresponding to all of the replica set members:

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

Note

The MongoClient constructor is non-blocking. When you connect to a replica set, the constructor returns immediately while the client uses background threads to connect to the replica set.

If you construct a MongoClient and immediately print the string representation of its nodes attribute, the list might be empty while the client connects to the replica-set members.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Stable API