binary – Tools for representing binary data to be stored in MongoDB

Tools for representing BSON binary data.


BSON binary subtype for binary data.

This is becomming the default subtype and should be the most commonly used.

New in version 1.5.


BSON binary subtype for functions.

New in version 1.5.


Old BSON binary subtype for binary data.

This is still the default subtype, but that is changing to BINARY_SUBTYPE.

New in version 1.7.


BSON binary subtype for a UUID.

uuid.UUID instances will automatically be encoded by bson using this subtype.

New in version 1.5.


BSON binary subtype for an MD5 hash.

New in version 1.5.


BSON binary subtype for any user defined structure.

New in version 1.5.

class bson.binary.Binary(data[, subtype=BINARY_SUBTYPE])

Bases: str

Representation of BSON binary data.

This is necessary because we want to represent Python strings as the BSON string type. We need to wrap binary data so we can tell the difference between what should be considered binary data and what should be considered a string when we encode to BSON.

Raises TypeError if data is not an instance of str or subtype is not an instance of int. Raises ValueError if subtype is not in [0, 256).

  • data: the binary data to represent
  • subtype (optional): the binary subtype to use
Subtype of this binary data.

Previous topic

bson – BSON (Binary JSON) Encoding and Decoding

Next topic

code – Tools for representing JavaScript code

This Page