Mongo and Kotlin

I’ve recently been using Kmongo library and Kotlin together however I’ve made an issue on Kmongo but I’m unsure it is related to the library.

I’m trying to persist my data onto my mongo database (version 4.2.2)

data class Person(val firstname: String, val lastname: String){

   val fullName
     get() = "$lastname $firstname"
}

When I insert the data I sent only an object like this : val personData = Person("John", "Doe")
but when I do check on my mongo database

db.persons.find()
> { "_id" : ObjectId("5e2da298159243f9894d3834"), "firstname" : "John", "lastname" : "Doe", "fullName" : "Doe John" }

How can I prevent to get fullName saved in my database ?

EDIT:

Insert code:

fun insertPerson(personData: Person): Person {
        return userRepository.savePerson(personData)
    }

With my Webservice I simply call this function where personData is the object I declared previously

You showed everything but the insert code.

Updated with the insert code

Cheers!

I know nothing of Kotlin or KMongo but it’s very likely to be your getter as the culprit and not KMongo. I’ve just had a quick look at the doc and it seems that when called, your data class will naturally return the input properties/variables declared within and in addition, your getter (get()) will also be returned.

Yeah but delegated parameter such as fullName are by default transient, and shouldn’t be persisted.

When I tried to annotate my field with @Transient I had an inspection message saying : Property does not have backing field which makes it non-serializable and therefore @Transient is redundant

That’s where I’m getting confused.

Maybe something like this:

   @Transient val fullName : String = "$lastname $firstname"
     get() = field

I just tried but no :confused: didn’t do the trick.

I might just let that computed property in my database, it could be useful later and avoid doing firstname+lastname in query

What was the error message this time round?

No error, it did persist the object but still with the fullNumber value in the database

Just as a sanity check, drop the collection and insert again. And just be sure it’s running the refactored code.

Also try this:

   @Transient val fullName : String = "$lastname $firstname"

… minus the getter

I already suppressed the getter, my IDE was telling it is redundant.

I just tried with dropping my collection but it didn’t work :’(

Try another… keep the @Transient annotation and annotate the class as @Serializable:

import kotlinx.serialization.*

@Serializable
data class Person(...

I tried with configuration like this :

  • @Serializable and @Transient
  • @Serializable

but none worked :confused: I even drop the collection between the change to be sure

Try these annotations:

It finally worked ! I was looking into Kmongo library and it uses Jackson in the back.

I used

@Serialiable
@JsonIgnoreProperties("fullName")
data class Person(...

Yep, that was what I learnt. The other Transient property is for the JVM. :+1:

Thank you very much :slight_smile: