Docs Menu
Docs Home
/
MongoDB Drivers
/

Kotlin Sync Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Stable API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

Welcome to the documentation site for the Kotlin Sync Driver, the official MongoDB driver for synchronous Kotlin applications.

  • API Documentation

  • Source Code

Tip

Other Kotlin Platforms for MongoDB

If your Kotlin application requires asynchronous processing and uses coroutines, you can use the official Kotlin Coroutine Driver instead of the Sync Driver.

If you are developing an Android or Kotlin Multiplatform (KMP) application, you can use the MongoDB Atlas Device Kotlin SDK to access Atlas App Services and to manage your Realm data.

The recommended way to get started using the driver in your project is with a dependency management system.

If you are using Gradle, add the following to your build.gradle.kts dependencies list:

build.gradle.kts
dependencies {
implementation("org.mongodb:mongodb-driver-kotlin-sync:5.1.0")
}

If you are using Maven, add the following to your pom.xml dependencies list:

pom.xml
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-kotlin-sync</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

import com.mongodb.ConnectionString
import com.mongodb.kotlin.client.sync.MongoClient
import com.mongodb.kotlin.client.sync.MongoDatabase
import com.mongodb.MongoClientSettings
// Replace the placeholder with your Atlas connection string
val uri = "<connection string>"
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString(uri))
.retryWrites(true)
.build()
// Create a new client and connect to the server
val mongoClient = MongoClient.create(settings)
val database = mongoClient.getDatabase("test")

Note

For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.

You can use the Stable API feature starting with MongoDB Server version 5.0 and Kotlin Sync Driver. When you use the Stable API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.

Note

Starting from February 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.

To use this feature, construct a MongoDB client instance, specifying a version of the Stable API:

import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.ServerApi
import com.mongodb.ServerApiVersion
import com.mongodb.kotlin.client.sync.MongoClient
import com.mongodb.kotlin.client.sync.MongoDatabase
// Replace the placeholder with your MongoDB deployment's connection string
val uri = "<connection string>"
// Set the Stable API version on the client
val serverApi = ServerApi.builder()
.version(ServerApiVersion.V1)
.build()
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString(uri))
.serverApi(serverApi)
.build()
// Create a new client and connect to the server
val mongoClient = MongoClient.create(settings)
val database = mongoClient.getDatabase("test")

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.

  2. Install and configure MongoDB Server.

  3. Start the server.

Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

The following compatibility table specifies the recommended version(s) of the MongoDB Kotlin Sync driver for use with a specific version of MongoDB.

The first column lists the driver version.

Important

MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.

Icon
Explanation
All features are supported.
The Driver version will work with the MongoDB version, but not all new MongoDB features are supported.
No mark
The Driver version is not tested with the MongoDB version.
Kotlin Sync Driver Version
MongoDB 7.0
MongoDB 6.1
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
5.1
5.0
4.11
4.10

The driver does not support older versions of MongoDB.

The MongoDB Kotlin Sync driver requires Kotlin 1.8 or later.

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

← MongoDB Kotlin Drivers