Skip to content

ConditionalValueOperators

Operators to conditionally create a value.

To learn more about aggregation operators, view AggregationOperators.

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

cond

open fun <R : Any, T> cond(
    condition: Value<R, Boolean>, 
    ifTrue: Value<R, T>, 
    ifFalse: Value<R, T>
): Value<R, T>

Decides between two values depending on the evaluation of a boolean value.

Example
class User(
    val name: String,
    val score: Int,
    val multiplier: Int,
)

users.updateManyWithPipeline {
    set {
        User::score set cond(
            condition = of(User::multiplier) gt of(2),
            ifTrue = of(User::score) * of(User::multiplier),
            ifFalse = of(User::score)
        )
    }
}
External resources

See also

div

open operator fun <Context : Any, Root, Child> Value<Context, Root>.div(field: Field<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
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

open operator fun <Root, Type> KProperty1<Root, Collection<Type>>.get(index: Int): Field<Root, Type>

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

open operator fun <Root, Type> KProperty1<Root, Map<String, Type>>.get(index: String): Field<Root, Type>

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

open 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.

open 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.

of

open fun <Context : Any, Result> of(field: Field<Context, Result>): Value<Context, Result>

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)
    }
}

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): 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)
    }
}

switch

open fun <R : Any, T> switch(vararg cases: Pair<Value<R, Boolean>, Value<R, T>>, default: Value<R, T>? = null): Value<R, T>

Selects one value based on multiple conditions.

Example
class User(
    val name: String,
    val score: Int,
    val role: String,
    val bonus: Int?,
)

users.updateManyWithPipeline {
    set {
        User::bonus set switch(
            of(User::role) eq of("GUEST") to of(5),
            of(User::role) eq of("EMPLOYEE") to of(6),
            of(User::role) eq of("ADMIN") to of(7),
            default = of(-1)
        )
    }
}
External resources

See also

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.