encode

abstract fun <T : Any> encode(obj: T, type: KType): BsonDocument(source)

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

Prefer using the overload with a single parameter.

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.