Field

interface Field<in Root, out Type>(source)

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:

collection.find {
User::profile / Profile::name eq "Thibault Lognaise"
}

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

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
abstract val path: Path

Low-level representation of this field's path.

Functions

Link copied to clipboard
open operator fun <Child> div(child: KProperty1<in Type & Any, Child>): Field<Root, Child>
open operator fun <Child> div(child: Field<Type, Child>): Field<Root, Child>

Refers to child as a nested field of the current field.

Link copied to clipboard
operator fun <Root, Type> Field<Root, Collection<Type>>.get(index: Int): Field<Root, Type>

Refers to a specific item in an array, by its index.

operator fun <Root, Type> Field<Root, Map<String, Type>>.get(key: String): Field<Root, Type>

Refers to a specific item in a map, by its name.

Link copied to clipboard
open fun <Child> unsafe(child: String): Field<Root, Child>

Refers to a field child of the current field, with no compile-time safety.