Skip to content

Kotlin BSON • Multiplatform abstraction for different BSON implementationsopensavvy.ktmongo.bsonBsonContextbuildDocument

buildDocument

@LowLevelApi
abstract fun buildDocument(block: BsonFieldWriter.() -> Unit): Bson

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")
        }
    }
}
@LowLevelApi
open fun buildDocument(instance: BsonFieldWriteable): Bson

Instantiates a new BSON document representing the provided instance.

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

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.