Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.aggregation.operatorsConditionalValueOperatorsswitch

switch

open fun <R : Any, T> switch(vararg cases: <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