Package-level declarations

Syntax to refer to specific fields in MongoDB documents.

When creating a query, we need to refer to specific fields in a MongoDB document, which may be in a nested document or in an array. Traditionally, this is done with the string representation of the path, but that leads to brittle code that is hard to navigate and refactor. Instead, KtMongo provides a DSL to refer to fields with type-safety:

User::_id                          // _id
User::profile / Profile::name // profile.name
User::friends[1] / Friend::name // friends.1.name

The operators are implemented in the interface FieldDsl which is already brought into scope by all KtMongo methods, and return instances of Field, the type-safe representation of a MongoDB path.

Operators

Types

Link copied to clipboard
interface Field<in Root, out Type>

High-level, typesafe pointer to a specific field in a document.

Link copied to clipboard
interface FieldDsl

Operators to construct MongoDB field references from Kotlin code.

Link copied to clipboard
data class Path(val segment: PathSegment, val parent: Path?)

Low-level, type-unsafe pointer to a specific field in a document.

Link copied to clipboard

Single segment in a Path.

Link copied to clipboard

Allows configuring how the DSL generates MongoDB paths from KProperty1 instances.

Functions

Link copied to clipboard
infix inline fun <T> Bson.at(field: Field<*, T>): T

Finds the first value that matches path in a given BSON document.

Link copied to clipboard

Returns a new Path instance that is the concatenation of the current path and a child path.

operator fun Path.div(segment: PathSegment): Path

Returns a new Path instance that is the concatenation of the current path and a segment.

Link copied to clipboard

Creates an instance of FieldDsl.

Link copied to clipboard

Creates a root path from the provided root field name.

Link copied to clipboard
inline fun <T> Bson.select(field: Field<*, T>): Sequence<T>

Finds all values that match field in a given BSON document.

Link copied to clipboard
inline fun <T> Bson.selectFirst(field: Field<*, T>): T

Finds the first value that matches field in a given BSON document.

Link copied to clipboard

Converts this MongoDB Field to a BsonPath.

Converts this MongoDB Path to a BsonPath.