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

  • bson.binary.BINARYSUBTYPE = 0_
  • BSON binary subtype for binary data.

This is the default subtype for binary data.

  • bson.binary.FUNCTIONSUBTYPE = 1_
  • BSON binary subtype for functions.
  • bson.binary.OLDBINARY_SUBTYPE = 2_
  • Old BSON binary subtype for binary data.

This is the old default subtype, the currentdefault is BINARY_SUBTYPE.

  • bson.binary.OLDUUID_SUBTYPE = 3_
  • Old BSON binary subtype for a UUID.

uuid.UUID instances will automatically be encodedby bson using this subtype.

New in version 2.1.

  • bson.binary.UUIDSUBTYPE = 4_
  • BSON binary subtype for a UUID.

This is the new BSON binary subtype for UUIDs. Thecurrent default is OLD_UUID_SUBTYPE.

Changed in version 2.1: Changed to subtype 4.

  • bson.binary.STANDARD = 4
  • The standard UUID representation.

uuid.UUID instances will automatically be encoded toand decoded from BSON binary, using RFC-4122 byte order withbinary subtype UUID_SUBTYPE.

New in version 3.0.

  • bson.binary.PYTHONLEGACY = 3_
  • The Python legacy UUID representation.

uuid.UUID instances will automatically be encoded toand decoded from BSON binary, using RFC-4122 byte order withbinary subtype OLD_UUID_SUBTYPE.

New in version 3.0.

  • bson.binary.JAVALEGACY = 5_
  • The Java legacy UUID representation.

uuid.UUID instances will automatically be encoded toand decoded from BSON binary subtype OLD_UUID_SUBTYPE,using the Java driver’s legacy byte order.

Changed in version 3.6: BSON binary subtype 4 is decoded using RFC-4122 byte order.

New in version 2.3.

  • bson.binary.CSHARPLEGACY = 6_
  • The C#/.net legacy UUID representation.

uuid.UUID instances will automatically be encoded toand decoded from BSON binary subtype OLD_UUID_SUBTYPE,using the C# driver’s legacy byte order.

Changed in version 3.6: BSON binary subtype 4 is decoded using RFC-4122 byte order.

New in version 2.3.

  • bson.binary.MD5SUBTYPE = 5_
  • BSON binary subtype for an MD5 hash.
  • bson.binary.USERDEFINED_SUBTYPE = 128_
  • BSON binary subtype for any user defined structure.
  • 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 asthe BSON string type. We need to wrap binary data so we can tellthe difference between what should be considered binary data andwhat should be considered a string when we encode to BSON.

Raises TypeError if data is not an instance of bytes(str in python 2) or subtype is not an instance ofint. Raises ValueError if subtype is not in [0, 256).

Note

In python 3 instances of Binary with subtype 0 will be decodeddirectly to bytes.

Parameters:

  • data: the binary data to represent. Can be any bytes-like typethat implements the buffer protocol.
  • subtype (optional): the binary subtypeto use

Changed in version 3.9: Support any bytes-like type that implements the buffer protocol.

  • subtype
  • Subtype of this binary data.

UUID wrapper to support working with UUIDs stored as PYTHON_LEGACY.

  1. >>> import uuid
  2. >>> from bson.binary import Binary, UUIDLegacy, STANDARD
  3. >>> from bson.codec_options import CodecOptions
  4. >>> my_uuid = uuid.uuid4()
  5. >>> coll = db.get_collection('test',
  6. ... CodecOptions(uuid_representation=STANDARD))
  7. >>> coll.insert_one({'uuid': Binary(my_uuid.bytes, 3)}).inserted_id
  8. ObjectId('...')
  9. >>> coll.count_documents({'uuid': my_uuid})
  10. 0
  11. >>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)})
  12. 1
  13. >>> coll.find({'uuid': UUIDLegacy(my_uuid)})[0]['uuid']
  14. UUID('...')
  15. >>>
  16. >>> # Convert from subtype 3 to subtype 4
  17. >>> doc = coll.find_one({'uuid': UUIDLegacy(my_uuid)})
  18. >>> coll.replace_one({"_id": doc["_id"]}, doc).matched_count
  19. 1
  20. >>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)})
  21. 0
  22. >>> coll.count_documents({'uuid': {'$in': [UUIDLegacy(my_uuid), my_uuid]}})
  23. 1
  24. >>> coll.find_one({'uuid': my_uuid})['uuid']
  25. UUID('...')

Raises TypeError if obj is not an instance of UUID.

Parameters:

  • obj: An instance of UUID.
  • uuid
  • UUID instance wrapped by this UUIDLegacy instance.