codecRegistry

val codecRegistry: CodecRegistry(source)

The CodecRegistry used by this factory to create and read documents.

This registry includes codecs for KtMongo-specific types. When mixing KtMongo with the official driver, it is recommended to configure the official driver using this registry to ensure types are serialized identically by both libraries.

val client = MongoClient.create("mongodb://mongo:27017")
val factory = BsonFactory(client.codecRegistry)
val clientWithKotlin = client.withCodecRegistry(factory.codecRegistry)

val database = clientWithKotlin.getDatabase("myDatabase")
val collection = database.getCollection<MyType>("myCollection").asKtMongo()

If you only use the KtMongo DSL to write queries, you do not need this configuration.