Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.aggregation.operatorsTypeValueOperatorstoBoolean

toBoolean

open fun <R : Any> Value<R, *>.toBoolean(): Value<R, Boolean>

Converts this value to a BsonType.Boolean.

Conversion algorithm

BsonType.Boolean is returned as itself.

Numeric types (BsonType.Int32, BsonType.Int64, BsonType.Double and BsonType.Decimal128) consider that 0 is false and all other values are true.

BsonType.Null always returns null.

All other types always return true.

Example

class User(
    val foo: String
)

users.aggregate()
    .project {
        Field.unsafe<Boolean>("asBoolean") set of(User::foo).toBoolean()
    }

External resources

See also