Cann't use Kotlinx DateTime with Kotlin MongoDB Driver

So KMongo for Kotlin is deprecated because of the new official driver (which has been around since 2016)

I’m trying to insert data to the database using an instance of data class that has Instant and LocalDateTime in it

@Serializable
data class User(
    val createdAt: Instant,
    val updatedAt: Instant,
)
 

But I’m getting the following exception:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for CodecCacheKey\{clazz=class kotlinx.datetime.Instant, types=null}.
at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:87)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
at org.bson.internal.ChildCodecRegistry.get(ChildCodecRegistry.java:68)
at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:226)
at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:199)
at org.bson.codecs.kotlin.DataClassCodec$Companion.create$bson_kotlin(DataClassCodec.kt:148)
at org.bson.codecs.kotlin.DataClassCodecProvider.get(DataClassCodecProvider.kt:28)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at com.mongodb.KotlinCodecProvider.get(KotlinCodecProvider.java:83)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:70)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at org.bson.codecs.configuration.OverridableUuidRepresentationCodecProvider.get(OverridableUuidRepresentationCodecProvider.java:47)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
at org.bson.internal.ChildCodecRegistry.get(ChildCodecRegistry.java:68)
at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:226)
at org.bson.codecs.kotlin.DataClassCodec$Companion.getCodec(DataClassCodec.kt:199)
at org.bson.codecs.kotlin.DataClassCodec$Companion.create$bson_kotlin(DataClassCodec.kt:148)
at org.bson.codecs.kotlin.DataClassCodecProvider.get(DataClassCodecProvider.kt:28)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at com.mongodb.KotlinCodecProvider.get(KotlinCodecProvider.java:83)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:70)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at org.bson.codecs.configuration.OverridableUuidRepresentationCodecProvider.get(OverridableUuidRepresentationCodecProvider.java:47)
at org.bson.internal.ProvidersCodecRegistry.getFromCodecProvider(ProvidersCodecRegistry.java:95)
at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:82)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:80)
at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:50)
at com.mongodb.internal.operation.Operations.getCodec(Operations.java:746)
at com.mongodb.internal.operation.Operations.bulkWrite(Operations.java:466)
at com.mongodb.internal.operation.Operations.insertOne(Operations.java:392)
at com.mongodb.internal.operation.AsyncOperations.insertOne(AsyncOperations.java:202)
at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.lambda$insertOne$6(MongoOperationPublisher.java:261)
at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.createWriteOperationMono(MongoOperationPublisher.java:446)
at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.createSingleWriteRequestMono(MongoOperationPublisher.java:454)
at com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.insertOne(MongoOperationPublisher.java:261)
at com.mongodb.reactivestreams.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:367)
at com.mongodb.kotlin.client.coroutine.MongoCollection.insertOne(MongoCollection.kt:627)
at com.mongodb.kotlin.client.coroutine.MongoCollection.insertOne$default(MongoCollection.kt:626)

I don’t know how to create a codec for Kotlinx DateTime types and I can’t find one, so can anyone tell me how to do it? thank you for your time and efforts.

the official docs says everything that is Serializable can convert to BSON format in efficient way. is there any chance I’m wrong?

Hey! I saw that JAVA-5296 was filed regarding this issue. We’ll chime in on that ticket with updates.

2 Likes

Actually that’s me, I did it on three places, stack overflow, MongoDB community and MongoDB issues

I also found a workaround and add it as a workaround answer in stack overflow