Field
High-level, typesafe pointer to a specific field in a document.
This type may refer to fields nested in children documents or arrays. Instances are generally used to refer to a MongoDB field we want to read or update, when writing requests.
Type safety
This type is responsible for ensuring type safety:
class User(
val _id: ObjectId,
val profile: Profile,
val friends: List<Friend>,
)
class Profile(
val name: String,
)
class Friend(
val name: String,
val age: Int,
)
// Refer to the user's id
println(User::_id.field)
// Refer to the user's name
println(User::profile / Profile::name)
// Refer to the name of the second friend
println(User::friends[1] / Friend::name)
Content copied to clipboard
Some of the functions of the DSL may be available only when FieldDsl is in scope. All operator scopes provided by this library should bring it into scope automatically.
For example, when writing a filter, methods from this interface are automatically available:
collection.find {
User::profile / Profile::name eq "Thibault Lognaise"
}
Content copied to clipboard
To bypass type-safety, and refer to arbitrary fields without declaring them first, see unsafe.
Parameters
Root
The type of the document in which this field is in.
Type
The type of the value stored by this field.