Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.aggregation.operatorsConditionalValueOperatorscond

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