Skip to content

ValueOperators

interface ValueOperators : FieldDsl

Supertype for all interface operators describing operators on aggregation values.

Most of the time, end-users will be using the subtype AggregationOperators instead of this interface.

Inheritors

Properties

context

@LowLevelApi
abstract override val context: BsonContext

The strategy used when converting from KProperty1 to Field.

field

Converts a Kotlin property into a Field.

Functions

div

open operator fun <Context : Any, Root, Child> Value<Context, Root>.div(field: Field<Root, Child>): Value<Context, Child>

open operator fun <Context : Any, Root, Child> Value<Context, Root>.div(field: KProperty1<Root, Child>): Value<Context, Child>

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

Examples

class User(
    val name: String,
)

class Data(
    val users: List<User>,
    val userNames: List<Int>,
)

data.aggregate()
    .set {
        Data::userNames set Data::users.map { it / User::name }
    }

External resources

get

@JvmName(name = "bsonField")
operator fun <Context : Any, Child> Value<Context, BsonDocument>.get(field: String): Value<Context, Child>

@JvmName(name = "bsonItem")
operator fun <Context : Any, Child> Value<Context, BsonArray>.get(index: Int): Value<Context, Child>

Refers to a child field of a field of type BsonDocument.

Example

class User(
    val _id: ObjectId,
    val name: String,
    val externalData: BsonDocument,
)

users.find {
    User::externalData.get<Profile?>("profile") ne null
}
open operator fun <Context : Any, Result> Value<Context, Collection<Result>>.get(index: Value<Context, Int>): Value<Context, Result>

open operator fun <Context : Any, Result> Value<Context, Collection<Result>>.get(index: Int): Value<Context, Result>

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

Examples

class Pet(
    val name: String,
    val age: Int,
)

class User(
    val pets: List<Pet>,
    val favorite: Pet,
)

users.aggregate()
    .set {
        User::favorite set User::pets[0] / Pet::name
    }

External resources

of

Refers to a field within an aggregation value.

Example

class Product(
    val acceptanceDate: Instant,
    val publishingDate: Instant,
)

val publishedBeforeAcceptance = products.find {
    expr {
        of(Product::publishingDate) lt of(Product::acceptanceDate)
    }
}
open fun <Result> of(value: Result, type: KType): Value<Any, Result>

inline fun <Result> of(value: Result): Value<Any, Result>

Refers to a Kotlin value within an aggregation value.

Example

class Product(
    val age: Int,
)

val publishedBeforeAcceptance = products.find {
    expr {
        of(Product::age) lt of(15)
    }
}
open fun of(value: BsonType): Value<Any, BsonType>

Refers to a BsonType within an aggregation value.

Example

class Product(
    val age: Int,
)

val publishedBeforeAcceptance = products.find {
    expr {
        of(Product::age).type eq of(BsonType.Int32)
    }
}

unsafe

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: String): Field<Root, Child>

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

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: Field<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Child> Field<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Child> Field<Root, *>.unsafe(child: Field<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Type, Child> Field<Root, Type>.unsafe(child: String): Field<Root, Child>

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

unsafeCast

Overwrites the represented type of this value.

This method can be useful to bypass type checks.

Example

If a field has changed type over time, the DTO will use the newer type. However, you may still want to write a query using the old type:

class User(
    val _id: ObjectId,
    val name: String,
    val age: Int,
)

users.filter { User::age hasType BsonType.Double }
    .updateManyWithPipeline {
        set {
            User::age set User::age.unsafeCast<Double>() // Reflect the old type
               .toInt() // Convert to the new type
        }
    }