Skip to content

ArithmeticValueOperators

Operators to arithmetically combine two or more values.

To learn more about aggregation operators, see 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

abs

open fun <Context : Any, Result : Number?> abs(value: Value<Context, Result>): Value<Context, Result>

The absolute value of a number.

If the value is null or NaN, it is returned unchanged.

Example
class Sensor(
    val name: String,
    val startTemp: Int,
    val endTemp: Int,
    val diffTemp: Int,
)

collection.updateManyWithPipeline(filter = { Sensor::diffTemp.isNull() }) {
    set {
        Sensor::diffTemp set abs(of(Sensor::startTemp) - of(Sensor::endTemp))
    }
}
External resources

ceil

open fun <Context : Any, Result : Number?> ceil(value: Value<Context, Result>): Value<Context, Result>

The smallest integer greater than or equal to the specified value.

If the value is null or NaN, it is returned unchanged.

Example
class Sensor(
    val value: Double,
    val maxBound: Double,
)

collection.aggregate()
    .set {
        Sensor::maxBound set ceil(of(Sensor::value))
    }.toList()
External resources

div

open operator fun <Context : Any, Result> Value<Context, Result>.div(other: Value<Context, Result>): Value<Context, Result>

Divides one aggregation value by another.

Example
class ConferencePlanning(
    val hours: Int,
    val workdays: Double,
)

collection.updateManyWithPipeline {
    set {
        ConferencePlanning::workdays set (of(ConferencePlanning::hours) / of(8))
    }
}
External resources

floor

open fun <Context : Any, Result : Number?> floor(value: Value<Context, Result>): Value<Context, Result>

The largest integer less than or equal to the specified value.

If the value is null or NaN, it is returned unchanged.

Example
class Sensor(
    val value: Double,
    val minBound: Double,
)

collection.aggregate()
    .set {
        Sensor::minBound set floor(of(Sensor::value))
    }.toList()
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.

minus

open operator fun <Context : Any, Result> Value<Context, Result>.minus(other: Value<Context, Result>): Value<Context, Result>

Subtracts one aggregation value from another.

The second argument is subtracted from the first argument.

Example
class Sale(
    val price: Int,
    val fee: Int,
    val discount: Int,
    val total: Int,
)

collection.updateManyWithPipeline {
    set {
        Sale::total set (of(Sale::price) + of(Sale::fee) - of(Sale::discount))
    }
}
External resources

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

plus

open operator fun <Context : Any, Result> Value<Context, Result>.plus(other: Value<Context, Result>): Value<Context, Result>

Sums two aggregation values.

Example
class Product(
    val name: String,
    val price: Int,
    val dailyPriceIncrease: Int,
)

collection.updateManyWithPipeline {
    set {
        Product::price set (of(Product::price) + of(Product::dailyPriceIncrease))
    }
}
External resources

times

open operator fun <Context : Any, Result> Value<Context, Result>.times(other: Value<Context, Result>): Value<Context, Result>

Multiplies two or more aggregation values.

Example
class Sale(
    val price: Double,
    val quantity: Int,
    val total: Double,
)

collection.updateManyWithPipeline {
    set {
        Sale::total set (of(Sale::price) * of(Sale::quantity))
    }
}
External resources

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.