buildDocument

Instantiates a new BSON document.

Example

To create the following BSON document:

{
"name": "Bob",
"isAlive": true,
"children": [
{
"name": "Alice"
},
{
"name": "Charles"
}
]
}

use the code:

buildDocument {
writeString("name", "Alice")
writeBoolean("isAlive", true)
writeArray("children") {
writeDocument {
writeString("name", "Alice")
}
writeDocument {
writeString("name", "Charles")
}
}
}

Instantiates a new BSON document representing the provided instance.


abstract fun <T : Any> buildDocument(obj: T, type: KType, klass: KClass<T>): Bson(source)

Writes an arbitrary Kotlin obj into a top-level BSON document.

Prefer using BsonContext.write.

A top-level BSON document cannot be null, cannot be a primitive, and cannot be a collection. If obj is not representable as a document, an exception is thrown.