Skip to content

Referring to nested documents

For the rest of this article, let's take the following example, in which the collection we're interested in is User:

class User(
    val name: String,
    val country: Country,
    val pets: List<Pet>,
)

class Country(
    val id: String,
    val code: String,
)

class Pet(
    val id: String,
    val name: String,
)

Referring to a non-nested field is identical with KMongo and KtMongo:

User::name eq "foo"

Referring to nested documents is identical with both libraries:

User::country / Country::code eq "FR"

Referring to a list item by index uses the get operator:

Using KMongo
User::pets.pos(4) / Pet::name eq "Chocolat"
Using KtMongo
User::pets[4] / Pet::name eq "Chocolat"