Skip to content

asKtMongo

fun MongoClient.asKtMongo(): CoroutineMongoClient

Instantiates a KtMongo CoroutineMongoClient using an existing client from the official Kotlin driver.

This method allows taking advantage of the full configuration power of the official client.

Example

import com.mongodb.kotlin.client.coroutine.MongoClient

fun main() = runBlocking {
    val client = MongoClient.create(/* … */)
         .asKtMongo()

     val database = client.database("my-app")
     val users = database.collection<UserDto>("users")

     println("Users: ${users.count()}")
 }
fun <Document : Any> MongoCollection<Document>.asKtMongo(
    factory: BsonFactory = BsonFactory(this.codecRegistry), 
    propertyNameStrategy: PropertyNameStrategy = PropertyNameStrategy.Default, 
    objectIdGenerator: ObjectIdGenerator = ObjectIdGenerator.Jvm(), 
    type: KType
): CoroutineMongoCollection<Document>

inline fun <Document : Any> MongoCollection<Document>.asKtMongo(
    factory: BsonFactory = BsonFactory(this.codecRegistry), 
    propertyNameStrategy: PropertyNameStrategy = PropertyNameStrategy.Default, 
    objectIdGenerator: ObjectIdGenerator = ObjectIdGenerator.Jvm()
): CoroutineMongoCollection<Document>

Instantiates a KtMongo CoroutineMongoCollection using an existing collection from the official Kotlin driver.

Example

import com.mongodb.kotlin.client.coroutine.MongoClient

fun main() = runBlocking {
    val client = MongoClient.create(/* … */)
     val database = client.database("my-app")
     val users = database.collection<UserDto>("users")
         .asKtMongo()

     println("Users: ${users.count()}")
 }
fun MongoDatabase.asKtMongo(): CoroutineMongoDatabase

Instantiates a KtMongo CoroutineMongoDatabase using an existing client from the official Kotlin driver.

Example

import com.mongodb.kotlin.client.coroutine.MongoClient

fun main() = runBlocking {
    val client = MongoClient.create(/* … */)
     val database = client.database("my-app")
         .asKtMongo()

     val users = database.collection<UserDto>("users")

     println("Users: ${users.count()}")
 }

Instantiates a KtMongo CoroutineMongoAggregateIterable using an existing flow from the official Kotlin driver.

Instantiates a KtMongo CoroutineMongoFindIterable using an existing flow from the official Kotlin driver.