toInt
Converts this value to an Int (BsonType.Int32).
Conversion algorithm
BsonType.Int32 is returned as itself.
BsonType.Int64 is converted to an Int if it fits into the range. Otherwise, an exception is thrown.
BsonType.Boolean becomes 1 if true
and 0 if false
.
BsonType.Double and BsonType.Decimal128 are truncated to an integer value. If this value does not fall in the valid range for an int, an exception is thrown.
BsonType.String is parsed as an int. Only base 10 numbers can be parsed. If the value is outside the range of a double, an exception is thrown. "-5"
and "123456"
are two valid examples. Floating-point numbers are not supported (use toDouble).
BsonType.Datetime returns the number of milliseconds since the epoch.
BsonType.Null always returns null
.
Other types throw an exception.
Example
class Product(
val quantity: Double,
)
users.aggregate()
.set {
Field.unsafe<Int>("quantity") set of(Product::quantity).toInt()
}
External resources
See also
Get the value's type.