toInstant

open fun <R : Any> Value<R, *>.toInstant(): Value<R, <Error class: unknown class>>(source)

Converts this value to an Instant (BsonType.Datetime).

Conversion algorithm

BsonType.Datetime is returned as itself.

BsonType.Int64, BsonType.Double and BsonType.Decimal128 are interpreted as a timestamp from the UNIX epoch in milliseconds: positive values happen after the epoch, negative values happen before the epoch.

BsonType.String is parsed using the ISO timestamp formats, for example:

  • "2018-03-20"

  • "2018-03-20T12:00:00Z"

  • "2018-03-20T12:00:00+0500"

BsonType.ObjectId and BsonType.Timestamp are represented by extracting their timestamp component (both have a precision of one second).

BsonType.Null always returns null.

Other types throw an exception.

Example

class User(
val modificationDate: Instant
)

// Update old data
users.updateManyWithPipeline(
filter = {
User::modificationType {
not {
hasType(BsonType.String)
}
}
}
) {
set {
User::name set of(User::name).toInstant()
}
}

External resources

See also

Get the value's type.