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

Tools for representing binary data to be stored in MongoDB.


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 pymongo.binary.Binary(data[, subtype=BINARY_SUBTYPE])

Bases: str

Representation of binary data to be stored in or retrieved from MongoDB.

This is necessary because we want to store 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

dbref – Tools for manipulating DBRefs (references to documents stored in MongoDB)

Next topic

objectid – Tools for working with MongoDB ObjectIds

This Page