0

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 :

  1. Replace field @BsonId val userId: Id<User> by val user: User in my Post model but I think it is not efficient.

  2. After fetching posts, requesting all associated users (with a distinct by id) but I think it is not efficient too.

François Legrand
  • 1,151
  • 1
  • 14
  • 25

0 Answers0