toText

open fun <R : Any> Value<R, *>.toText(): Value<R, String>(source)

Converts this value to a String.

Note: the MongoDB operator is called toString, but that name would be ambiguous in Kotlin because of Any.toString.

Conversion algorithm

BsonType.String is always returned as itself.

BsonType.Int32, BsonType.Int64, BsonType.Double, BsonType.Decimal128, BsonType.Boolean and BsonType.BinaryData are converted to a string.

BsonType.ObjectId returns its hexadecimal representation.

BsonType.Datetime is formatted in ISO.

BsonType.Null always returns null.

Example

class Product(
val _id: ObjectId,
val age: Double,
)

// Migrate ids which were accidentally written as strings:
products.updateManyWithPipeline {
_id set of(Product::age).toText()
}

External resources

See also

Get the value's type.