Skip to content

BsonFieldWriter

Generator of BSON document fields.

This interface is used to write fields in a BSON document.

To write generic values, see BsonValueWriter.

Instances of this interface are commonly obtained by calling the BsonFactory.encode function.

Functions

write

@LowLevelApi
abstract fun write(name: String, block: BsonValueWriter.() -> Unit)

writeArray

@LowLevelApi
abstract fun writeArray(name: String, block: BsonValueWriter.() -> Unit)

writeBinaryData

@LowLevelApi
abstract fun writeBinaryData(
    name: String, 
    type: UByte, 
    data: ByteArray
)

writeBoolean

@LowLevelApi
abstract fun writeBoolean(name: String, value: Boolean)

writeDateTime

@LowLevelApi
abstract fun writeDateTime(name: String, value: Long)

writeDBPointer

@LowLevelApi
abstract fun writeDBPointer(
    name: String, 
    namespace: String, 
    id: ByteArray
)

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeDecimal128

@LowLevelApi
abstract fun writeDecimal128(
    name: String, 
    low: Long, 
    high: Long
)

writeDocument

@LowLevelApi
abstract fun writeDocument(name: String, block: BsonFieldWriter.() -> Unit)

writeDouble

@LowLevelApi
abstract fun writeDouble(name: String, value: Double)

writeInstant

@LowLevelApi
open fun writeInstant(name: String, value: Instant)

Writes an Instant. Conversion function on top of writeDateTime.

writeInt32

@LowLevelApi
abstract fun writeInt32(name: String, value: Int)
@LowLevelApi
open fun writeInt32(name: String, value: Short)
@LowLevelApi
open fun writeInt32(name: String, value: Byte)

writeInt64

@LowLevelApi
abstract fun writeInt64(name: String, value: Long)

writeJavaScript

@LowLevelApi
abstract fun writeJavaScript(name: String, code: String)

writeJavaScriptWithScope

@LowLevelApi
abstract fun writeJavaScriptWithScope(name: String, code: String)

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeMaxKey

@LowLevelApi
abstract fun writeMaxKey(name: String)

writeMinKey

@LowLevelApi
abstract fun writeMinKey(name: String)

writeNull

@LowLevelApi
abstract fun writeNull(name: String)

writeObjectId

@LowLevelApi
abstract fun writeObjectId(name: String, id: ByteArray)
@LowLevelApi
abstract fun writeObjectId(name: String, id: ObjectId)

writeRegularExpression

@LowLevelApi
abstract fun writeRegularExpression(
    name: String, 
    pattern: String, 
    options: String
)

writeSafe

@LowLevelApi
abstract fun <T> writeSafe(
    name: String, 
    obj: T, 
    type: KType
)

@LowLevelApi
inline fun <T> writeSafe(name: String, obj: T)

Writes an arbitrary obj into a BSON document.

All nested values are escaped as necessary such that the result is a completely inert BSON document.

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 {
    writeSafe("user", User(ObjectId("69c93e17b96e83b72d11b734"), Profile("Bob", 30)))
}

val user = bson.decode<User>()

println(user._id)          // ObjectId(69c93e17b96e83b72d11b734)
println(user.profile.name) // Bob
println(user.profile.age)  // 30

writeString

@LowLevelApi
abstract fun writeString(name: String, value: String)

writeSymbol

@LowLevelApi
abstract fun writeSymbol(name: String, value: String)

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeTimestamp

@LowLevelApi
abstract fun writeTimestamp(name: String, value: Timestamp)

writeUndefined

@LowLevelApi
abstract fun writeUndefined(name: String)

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeVector

@LowLevelApi
open fun writeVector(name: String, vector: Vector)