Kotlin Sync Driver
On this page
Introduction
Welcome to the documentation site for the Kotlin Sync Driver, the official MongoDB driver for synchronous Kotlin applications.
If you need to make asynchronous calls between your application and MongoDB with coroutines, use the MongoDB Kotlin Coroutine Driver instead of the Sync Driver.
Installation
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:
dependencies { implementation("org.mongodb:mongodb-driver-kotlin-sync:4.11.0") }
If you are using Maven, add the following to
your pom.xml
dependencies list:
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-kotlin-sync</artifactId> <version>4.11.0</version> </dependency> </dependencies>
Connect to MongoDB Atlas
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.
Stable API
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")
Connect to a MongoDB Server on Your Local Machine
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:
Download the Community or Enterprise version of MongoDB Server.
Install and configure MongoDB Server.
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.
Compatibility
MongoDB Compatibility
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.
Compatibility Table Legend
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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
4.11 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
The driver does not support older versions of MongoDB.
Language Compatibility
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.
How to get help
Ask questions on our MongoDB Community Forums.
Visit our Support Channels.
See Issues & Help.