Connect to MongoDB
On this page
Overview
This page contains code examples that show how to use the Kotlin Sync driver to connect your application to MongoDB by specifying various settings.
Tip
Connection Options
To learn more about the connection options on this page, see the link provided in each section. You can also view the Specify Connection Options guide to learn more.
To use a connection example from this page, copy the code example into the
sample application or your own application.
Be sure to replace all placeholders in the code examples, such as
<hostname>
, with the relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the Kotlin Sync driver installed in your Maven or Gradle project.
Copy the following code and paste it into a new
.kt
file.Copy a code example from this page and paste it on the specified lines in the file.
1 import com.mongodb.kotlin.client.MongoClient 2 import org.bson.Document 3 4 fun main() { 5 6 // Start example code here 7 8 // End example code here 9 10 val database = mongoClient.getDatabase("admin") 11 12 val command = Document("ping", 1) 13 val commandResult = database.runCommand(command) 14 println("Pinged your deployment. You successfully connected to MongoDB!") 15 }
Connection
The following sections describe how to connect to different targets, such as a local instance of MongoDB or a cloud-hosted instance on Atlas.
Local Deployment
The following code shows the connection string to connect to a local instance of MongoDB:
val uri = "mongodb://localhost:27017/" val mongoClient = MongoClient.create(uri)
Atlas
The following code shows the connection string to connect to a deployment hosted on Atlas:
val uri = "mongodb+srv://<db_username>:<db_password>@<hostname/port>/?<options>" val mongoClient = MongoClient.create(uri)
Replica Set
The following code shows the connection string to connect to a replica set:
val uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>" val mongoClient = MongoClient.create(uri)
Transport Layer Security (TLS)
The following sections describe how to connect to MongoDB while enabling the TLS protocol. To learn more about using TLS with the Kotlin Sync driver, see Enable TLS on a Connection.
Enable TLS
The following tabs demonstrate how to enable TLS on a connection:
val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString("<connection string>")) .applyToSslSettings { builder -> builder.enabled(true) } .build() val mongoClient = MongoClient.create(settings)
val uri = "mongodb+srv://<db_username>:<db_password>@<cluster-url>?tls=true" val mongoClient = MongoClient.create(uri)
To learn more about enabling TLS, see Enable TLS in the TLS configuration guide.
Disable Hostname Verification
The following tabs demonstrate how to disable hostname verification when connecting by using TLS:
val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString("<connection string>")) .applyToSslSettings { builder -> builder.enabled(true) builder.invalidHostNameAllowed(true) } .build() val mongoClient = MongoClient.create(settings);
val uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?"tls=true&tlsAllowInvalidHostnames=true" val mongoClient = MongoClient.create(uri)
Network Compression
The following sections describe how to connect to MongoDB while specifying network compression algorithms.
Compression Algorithms
The following tabs demonstrate how to specify all available compressors while connecting to MongoDB:
val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString("<connection string>")) .compressorList( listOf( MongoCompressor.createSnappyCompressor(), MongoCompressor.createZlibCompressor(), MongoCompressor.createZstdCompressor()) ) .build() val mongoClient = MongoClient.create(settings)
val uri = ConnectionString("mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd") val mongoClient = MongoClient.create(uri)
zlib Compression Level
The following tabs demonstrate how to specify a compression level for
the zlib
compressor:
val zlib = MongoCompressor.createZlibCompressor() val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString(uri)) .compressorList(listOf(zlib.withProperty(MongoCompressor.LEVEL, <level>))) .build() val mongoClient = MongoClient.create(settings)
val uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?" + "compressors=zlib" + "zlibCompressionLevel=<zlib compression level>" val mongoClient = MongoClient.create(uri)
Server Selection
The following code shows a connection string that specifies a server selection function:
val client = MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>", server_selector=<selector function>)
Stable API
The following code shows how to specify Stable API settings within a
MongoClientSettings
instance:
val serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build() val uri = "<connection string>" val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString(uri)) .serverApi(serverApi) .build() val client = MongoClient.create(settings)
To learn more about the Stable API, see Stable API.