KtMongo: MongoDB driver for Kotlin • Synchronous
Blocking/synchronous driver for MongoDB, using a rich Kotlin DSL.
Configuration
Start by declaring a dependency on this module (see the common page for help).
This driver is built on the top of the official Kotlin driver. You can use the official tutorials to get started and learn how to connect to the database.
The most basic option is to connect to your local MongoDB instance running on localhost:27017, use:
class User(
val _id: ObjectId,
val name: String,
)
val client = MongoClient.create()
val database = client.getDatabase("my_project")
val collection = database.getCollection<User>("users").asKtMongo()Content copied to clipboard
Note the call to asKtMongo() which is the only difference from the official usage. From then on, all methods from this driver are available on the collection variable: see SyncMongoCollection.
This means you are able to use KtMongo DSLs within your existing repositories: simply convert into a KtMongo equivalent where you need KtMongo functionality.