Choose a Connection Target
On this page
Overview
In this guide, you can learn how to use a connection string and a MongoClient
object
to connect to different types of MongoDB deployments.
Atlas
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 Kotlin Sync driver to connect to an Atlas cluster. The
code also uses the serverApi()
method to specify a Stable API version.
// Defines Stable API version val serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build() // Uses MongoClientSettings to apply connection string and specify the Stable API version val settings = MongoClientSettings.builder() .applyConnectionString("<connection string URI>") .serverApi(serverApi) .build() val mongoClient = MongoClient.create(settings)
Local Deployments
To connect to a local MongoDB deployment, use localhost
as the hostname. By
default, the mongod
process runs on port 27017, though you can customize this for
your deployment.
The following code shows how to use the Kotlin Sync driver to connect to a local MongoDB deployment:
val settings = MongoClientSettings.builder() .applyConnectionString("mongodb://localhost:27017") .build() val mongoClient = MongoClient.create(settings)
Replica Sets
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 Kotlin Sync 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 thedirectConnection
parameter.Specify more than one host in the replica set.
The following examples show how to connect to a MongoDB replica set running on port
27017
of three different hosts by using either the ConnectionString
or
MongoClientSettings
class. Select the tab that corresponds to your preferred class.
val mongoClient = MongoClient.create("mongodb://host1:27017,host2:27017,host3:27017/")
val hosts = listOf( ServerAddress("host1", 27017), ServerAddress("host2", 27017), ServerAddress("host3", 27017) ) val settings = MongoClientSettings.builder() .applyToClusterSettings { builder -> builder.hosts(hosts) } .build() val mongoClient = MongoClient.create(settings)
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.