Mongo和Kotlin的委托问题

我最近一起使用过Kmongo library和Kotlin,但是我在Kmongo上遇到了问题,但是不确定它是否与库有关。

我正在尝试将数据持久保存到mongo数据库(版本4.2.2)

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

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

当我插入数据时,我只发送了一个像这样的对象:val person = Person("John","Doe") 但是当我检查我的mongo数据库时

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

如何防止将fullName保存在数据库中?

编辑:

我尝试在变量上使用@Transient批注,但是它不起作用,并且收到一条检查消息,内容为:Property does not have backing field which makes it non-serializable and therefore @Transient is redundant

dong574233059 回答:Mongo和Kotlin的委托问题

今天,Kmongo库在后台使用Jackson,为避免解析属性,我不得不使用:https://www.concretepage.com/jackson-api/jackson-jsonignore-jsonignoreproperties-and-jsonignoretype

所以我的数据类的代码现在是:

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

使用@Transient批注:

@Transient
val fullName
  get() = "$lastname $firstname"
本文链接:https://www.f2er.com/2709959.html

大家都在问