Skip to content

KtMongo

object KtMongo

Entry-point to convert objects from the official drivers to the KtMongo library.

To learn more about the syntax of the different operators, see the top-level functions in this package.

Functions

from

@JvmStatic
@JvmOverloads
fun <T : Any> from(
    driver: MongoCollection<T>, 
    documentType: KType, 
    nameStrategy: PropertyNameStrategy = PropertyNameStrategy.Default
): JvmMongoCollection<T>

Converts a Java MongoDB collection into a KtMongo collection.

Example (Java)

try (var client = MongoClients.create()) {
    var javaCollection = client.getDatabase("my-db")
        .getCollection("my-col", MyCollection.class);

    var collection = KtMongo.from(javaCollection);

    System.out.println(
        collection.find().toList()
    );
}
@JvmStatic
@JvmOverloads
fun <T : Any> from(
    driver: MongoCollection<T>, 
    documentType: KType, 
    nameStrategy: PropertyNameStrategy = PropertyNameStrategy.Default
): JvmMongoCollection<T>

Converts a Kotlin MongoDB collection into a KtMongo collection.

Example (Kotlin)

MongoClients.create().use { client ->
    val kotlinCollection = it.getDatabase("my-db")
        .getCollection("my-col", MyCollection::class.java)

    val collection = KtMongo.from(kotlinCollection)

    println(
        collection.find().toList()
    )
}

typeOf

@JvmStatic
fun <T> typeOf(value: T): KType

Obtains a Kotlin KType instance for a Java value.

This method is a workaround required for now. In the future, when we move to stabilize the Java KtMongo driver, this will not be necessary anymore.