Kotlin KMongo library nested query not working

563 Views Asked by At

If nested property query is using, this is always returning null, I have no ideas why, just following offcial website document document link

database.getCollection<User>().findOne(User::auth / Auth::ssaid eq p.ssaid, User::auth / Auth::password eq p.password) 

no nested property query it is all fine.

database.getCollection<User>().findOne(User::lastlogin eq p.lastlogin)

model class code

data class Auth(val password: String, val ssaid: String, val localLastLogin: String)

data class User(
    @BsonProperty(useDiscriminator = true) val auth: Auth,
    val lastLogin: String = Instant.now().toString(),
    val members: List<Member> = emptyList(),
    val channels: List<Channel> = emptyList()
) : Data()
1

There are 1 best solutions below

0
On BEST ANSWER

You need to use @field:BsonProperty as follows:

data class User(
    @field:BsonProperty(useDiscriminator = true) val auth: Auth,
    val lastLogin: String = Instant.now().toString(),
    val members: List<Member> = emptyList(),
    val channels: List<Channel> = emptyList()
) : Data()

The reason being that there are multiple Java elements that are generated from the corresponding Kotlin element, and therefore multiple possible locations for the annotation in the generated Java bytecode. Therefore you need to be explicit about it. More details here.