I am working on an API with Ktor
and Kmongo
that returns a list of posts and each one is written by a user so my model looks like this :
data class Post(
@BsonId val id: ObjectId,
@BsonId val userId: Id<User>,
val content: String
)
data class User constructor(
@BsonId val id: ObjectId,
val email: String
)
I want a query that returns a list of posts with associated user (the whole user object). Is it possible to fetch associated users for each post with one kmongo
query ?
I tried multiple approaches :
Replace field
@BsonId val userId: Id<User>
byval user: User
in myPost
model but I think it is not efficient.After fetching posts, requesting all associated users (with a distinct by id) but I think it is not efficient too.