decode
Decodes this document into an instance of the Kotlin type T.
Serialization configuration
This method uses the serialization methods configured in the BsonFactory that created this instance.
For example, if you use the official Java or Kotlin MongoDB drivers, this method will use your configured CodecRegistry.
Example
data class User(
val _id: ObjectId,
val profile: Profile,
)
data class Profile(
val name: String,
val age: Int?,
)
val factory: BsonFactory = …
val bson = factory.buildDocument {
writeObjectId("_id", ObjectId("69c93e17b96e83b72d11b734"))
writeDocument("profile") {
writeString("name", "Bob")
writeInt32("age", 30)
}
}
val user = bson.decode<User>()
println(user._id) // ObjectId(69c93e17b96e83b72d11b734)
println(user.profile.name) // Bob
println(user.profile.age) // 30Content copied to clipboard
Overloads
Prefer using the parameter-less overload.
If type doesn't match T, the behavior is unspecified.
Throws
If the value cannot be decoded as an instance of T.