Interface BSONSerializeOptions

BSON Serialization options.

interface BSONSerializeOptions {
    bsonRegExp?: boolean;
    checkKeys?: boolean;
    enableUtf8Validation?: boolean;
    fieldsAsRaw?: Document;
    ignoreUndefined?: boolean;
    promoteBuffers?: boolean;
    promoteLongs?: boolean;
    promoteValues?: boolean;
    raw?: boolean;
    serializeFunctions?: boolean;
    useBigInt64?: boolean;
}

Hierarchy (view full)

Properties

bsonRegExp?: boolean

return BSON regular expressions as BSONRegExp instances.

false

checkKeys?: boolean

the serializer will check if keys are valid.

false

enableUtf8Validation?: boolean

Enable utf8 validation when deserializing BSON documents. Defaults to true.

fieldsAsRaw?: Document

allow to specify if there what fields we wish to return as unserialized raw buffer.

null

ignoreUndefined?: boolean

serialize will not emit undefined fields note that the driver sets this to false

true

promoteBuffers?: boolean

when deserializing a Binary will return it as a node.js Buffer instance.

false

promoteLongs?: boolean

when deserializing a Long will fit it into a Number if it's smaller than 53 bits.

true

promoteValues?: boolean

when deserializing will promote BSON values to their Node.js closest equivalent types.

true

raw?: boolean

Enabling the raw option will return a Node.js Buffer which is allocated using allocUnsafe API. See this section from the Node.js Docs here for more detail about what "unsafe" refers to in this context. If you need to maintain your own editable clone of the bytes returned for an extended life time of the process, it is recommended you allocate your own buffer and clone the contents:

const raw = await collection.findOne({}, { raw: true });
const myBuffer = Buffer.alloc(raw.byteLength);
myBuffer.set(raw, 0);
// Only save and use `myBuffer` beyond this point

Please note there is a known limitation where this option cannot be used at the MongoClient level (see NODE-3946). It does correctly work at Db, Collection, and per operation the same as other BSON options work.

serializeFunctions?: boolean

serialize the javascript functions

false

useBigInt64?: boolean

when deserializing a Long return as a BigInt.

false