unsafeCast
abstract override fun <NewType : Any> unsafeCast(type: KType): MultiplatformMongoCollection<NewType>(source)
Overwrites the represented type of this value.
This method can be useful to bypass type checks.
Example
If we know there are some documents that have the wrong format, we can use unsafeCast to find them without needing to add their fields to the production DTO.
class User(
val _id: ObjectId,
val name: String,
)
users.unsafeCast<BsonDocument>() // Treat all data as arbitrary BSON documents
.find { BsonDocument.get<String?>("oldField") ne null }
.forEach {
println(it["oldField"]?.decodeString())
}Content copied to clipboard