decodeElements

Decodes this array into a List of the Kotlin type T.

To decode this array into a type other than a List, see decode.

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 name: String,
val age: Int?,
)

val factory: BsonFactory = …

val bson = factory.buildDocument {
writeArray("users") {
writeDocument {
writeString("name", "Alice")
writeInt32("age", 13)
}

writeDocument {
writeString("name", "Bob")
writeInt32("age", 52)
}
}
}

val users = bson["users"]?.decodeElements<User>()

println(users[0].name) // Alice
println(users[1].age) // 13

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.