MongoDB request DSL • opensavvy.ktmongo.dsl.path • Field
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)
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:
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.
Types¶
Companion
¶
object Companion
Properties¶
path
¶
Low-level representation of this field's path.
Functions¶
div
¶
Refers to child
as a nested field of the current field.
Refers to child
as a nested field of the current field.
get
¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
unsafe
¶
Refers to a field child
of the current field, with no compile-time safety.