Skip to content

Instantly share code, notes, and snippets.

@hrothwell
Last active October 4, 2023 04:25
Show Gist options
  • Save hrothwell/6fbfce90fa7652526a52b10f9aa636fd to your computer and use it in GitHub Desktop.
Save hrothwell/6fbfce90fa7652526a52b10f9aa636fd to your computer and use it in GitHub Desktop.
Using Mongodb Kotlin drivers with default KMongo codec
/**
* Q: Why use KMongo's default codec over the official Mongodb Kotlin drivers' default codec or Kotlinx.serialization?
* A: Having been using KMongo for several years at an enterprise level, having to switch to a new codec or
* rework all of our data to be handled via this new codec seemed like more effort than it was worth at the moment.
*
* Q: Isn't KMongo deprecated? Why depend on it just for this?
* A: Yes it is deprecated, however, it is also assumed/expected that many users of KMongo will still be using
* KMongo's nicer / more intuitive query building tools / syntax. Until that query building logic is split off into its own project,
* we will still be pulling it in anyways. On top of this, I am just hopeful that the default codec that KMongo utilizes will also
* still be available to make transitions for others easier even if the base project is no longer available, fingers crossed!
*/
import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.kotlin.client.MongoClient
import org.litote.kmongo.KMongo
import org.litote.kmongo.service.ClassMappingType
import org.litote.kmongo.util.KMongoUtil
fun `using KMongo's default codec with the official Mongodb Kotlin drivers`() {
val connectionString = ConnectionString("mongodb://example:example@localhost:27017")
/**
* plain kmongo client, uses KMongo's default codec under the hood
* which includes Jackson serde among other things
*/
val kmongoClient = KMongo.createClient(connectionString)
/**
* plain mongo kotlin driver client. This utilizes a different default codec
* under the hood (MongoClientSettings.getDefaultCodecRegistry())
*/
val mongoDriverClient = MongoClient.create(connectionString)
/**
* What the default client settings would look like for the above client
*/
val defaultMongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build()
println(defaultMongoClientSettings.codecRegistry)
/**
* output:
* ProvidersCodecRegistry{
* codecProviders=[
* ValueCodecProvider{},
* BsonValueCodecProvider{},
* DBRefCodecProvider{},
* DBObjectCodecProvider{},
* DocumentCodecProvider{},
* CollectionCodecProvider{},
* IterableCodecProvider{},
* MapCodecProvider{},
* GeoJsonCodecProvider{},
* GridFSFileCodecProvider{},
* Jsr310CodecProvider{},
* JsonObjectCodecProvider{},
* BsonCodecProvider{},
* EnumCodecProvider{},
* com.mongodb.client.model.mql.ExpressionCodecProvider@60219a32,
* com.mongodb.Jep395RecordCodecProvider@63792a7a,
* com.mongodb.KotlinCodecProvider@6911005b
* ]
* }
*/
/**
* To utilize the codec that KMongo was using by default on top of the
* new Mongo Kotlin drivers we can pull it from an object that is exposed by KMongo
*/
val mongoClientSettingsWithKMongoCodec = MongoClientSettings.builder()
.codecRegistry(ClassMappingType.codecRegistry(KMongoUtil.defaultCodecRegistry))
.applyConnectionString(connectionString)
.build()
println(mongoClientSettingsWithKMongoCodec.codecRegistry)
/**
* output:
* ProvidersCodecRegistry{
* codecProviders=[
* ProvidersCodecRegistry{
* codecProviders=[
* ValueCodecProvider{},
* BsonValueCodecProvider{},
* DBRefCodecProvider{},
* DBObjectCodecProvider{},
* DocumentCodecProvider{},
* IterableCodecProvider{},
* MapCodecProvider{},
* GeoJsonCodecProvider{},
* GridFSFileCodecProvider{},
* Jsr310CodecProvider{},
* JsonObjectCodecProvider{},
* BsonCodecProvider{}
* ]
* },
* org.litote.kmongo.service.CustomCodecProvider@12223f2b,
* ProvidersCodecRegistry{
* codecProviders=[
* org.litote.kmongo.jackson.JacksonCodecProvider@1e42841a
* ]
* }
* ]
* }
*/
/**
* Should now be using the KMongo default codec, while still utilizing the official Kotlin drivers
* for connection. Should behave the exact same as KMongo in terms of serde
*/
val mongoDriverClientWithKMongoCodec = MongoClient.create(mongoClientSettingsWithKMongoCodec)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment