BsonFactory¶
interface BsonFactory
Entrypoint for creating BsonDocument and BsonArray instances.
Navigating BSON types¶
This interface is part of the BSON trinity:
-
BsonDocumentrepresents an entire BSON document. -
BsonArrayrepresents an array of BSON values. -
BsonValuerepresents a single value in isolation.
Serialization¶
This interface encapsulates the configuration of the serialization library in use.
For example, if you use the official java or Kotlin MongoDB drivers, all BSON values built by this factory will respect the configured CodecRegistry.
Usage¶
To create the following BSON document:
use the code:
val document = factory.buildDocument {
writeString("name", "Alice")
writeBoolean("isAlive", true)
writeArray("children") {
writeDocument {
writeString("name", "Alice")
}
writeDocument {
writeString("name", "Charles")
}
}
}
The value can then be read into Kotlin types:
Functions¶
buildArray¶
@LowLevelApi
abstract fun buildArray(block: BsonValueWriter.() -> Unit): BsonArray
@LowLevelApi
open fun buildArray(instance: BsonValueWriteable): BsonArray
Instantiates a new BSON array representing the provided instance.
buildDocument¶
@LowLevelApi
abstract fun buildDocument(block: BsonFieldWriter.() -> Unit): BsonDocument
@LowLevelApi
open fun buildDocument(instance: BsonFieldWriteable): BsonDocument
Instantiates a new BSON document representing the provided instance.
encode¶
@LowLevelApi
abstract fun <T : Any> encode(obj: T, type: KType): BsonDocument
encode¶
inline fun <T : Any> BsonFactory.encode(obj: T): BsonDocument
Writes an arbitrary Kotlin obj into a top-level BSON document.
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.
See also
BsonDocument.decode: The inverse operation.
readArray¶
@LowLevelApi
abstract fun readArray(bytes: ByteArray): BsonArray
Instantiates a new BSON array by reading its bytes representation.
The reverse operation is available as BsonArray.toByteArray.
readDocument¶
@LowLevelApi
abstract fun readDocument(bytes: ByteArray): BsonDocument
Instantiates a new BSON document by reading its bytes representation.
The reverse operation is available as BsonDocument.toByteArray.