Skip to content

MongoDB driver for Kotlin (synchronous, with Java helpers)

Blocking/synchronous driver for MongoDB Java users.

While KtMongo is a Kotlin-first library, we are prototyping its usage from Java. The KtMongo builder-based DSL can still be more convenient to use in Java than the official driver, because it deals with conditional requests more elegantly.

Note that this is a prototype. It is not complete and is not our priority for now. However, we welcome contributions.

Configuration

Add a dependency on dev.opensavvy.ktmongo:driver-sync-java.

In a Java project:

plugins {
    java
}

dependencies {
    implementation("dev.opensavvy.ktmongo:driver-sync:VERSION-HERE")
}

In a Kotlin JVM project:

plugins {
    kotlin("jvm")
}

dependencies {
    implementation("dev.opensavvy.ktmongo:driver-sync:VERSION-HERE")
}

Basic usage

Start by creating a com.mongodb.client.MongoCollection instance by following the instructions from the official Java driver.

Use the method KtMongo.from to convert it into a KtMongo collection.

Because Java doesn't provide optional parameters, and because Java and Kotlin lambdas are slightly different, we offer convenience methods.

collection.find(options(),filter(filter ->{
    filter.

eq(JavaField.of(Profile::name), "Fred");
    }));

You can find more complex examples in the test directory.

Java doesn't support operator overloading, so the User::profile / Profile::name syntax isn't possible. It is replaced by the JavaField class, which provides similar functionality.

Packages

opensavvy.ktmongo.sync