binary -- Tools for representing binary data to be stored in MongoDB#

bson.binary.BINARY_SUBTYPE = 0#

BSON binary subtype for binary data.

This is the default subtype for binary data.

bson.binary.FUNCTION_SUBTYPE = 1#

BSON binary subtype for functions.

bson.binary.OLD_BINARY_SUBTYPE = 2#

Old BSON binary subtype for binary data.

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

bson.binary.OLD_UUID_SUBTYPE = 3#

Old BSON binary subtype for a UUID.

uuid.UUID instances will automatically be encoded by bson using this subtype when using UuidRepresentation.PYTHON_LEGACY, UuidRepresentation.JAVA_LEGACY, or UuidRepresentation.CSHARP_LEGACY.

在 2.1 版本加入.

bson.binary.UUID_SUBTYPE = 4#

BSON binary subtype for a UUID.

This is the standard BSON binary subtype for UUIDs. uuid.UUID instances will automatically be encoded by bson using this subtype when using UuidRepresentation.STANDARD.

bson.binary.STANDARD = 4#

An alias for UuidRepresentation.STANDARD.

在 3.0 版本加入.

bson.binary.PYTHON_LEGACY = 3#

An alias for UuidRepresentation.PYTHON_LEGACY.

在 3.0 版本加入.

bson.binary.JAVA_LEGACY = 5#

An alias for UuidRepresentation.JAVA_LEGACY.

在 3.6 版本发生变更: BSON binary subtype 4 is decoded using RFC-4122 byte order.

在 2.3 版本加入.

bson.binary.CSHARP_LEGACY = 6#

An alias for UuidRepresentation.CSHARP_LEGACY.

在 3.6 版本发生变更: BSON binary subtype 4 is decoded using RFC-4122 byte order.

在 2.3 版本加入.

bson.binary.MD5_SUBTYPE = 5#

BSON binary subtype for an MD5 hash.

bson.binary.COLUMN_SUBTYPE = 7#

BSON binary subtype for columns.

在 4.0 版本加入.

bson.binary.SENSITIVE_SUBTYPE = 8#

BSON binary subtype for sensitive data.

在 4.5 版本加入.

bson.binary.USER_DEFINED_SUBTYPE = 128#

BSON binary subtype for any user defined structure.

class bson.binary.UuidRepresentation#
CSHARP_LEGACY = 6#

The C#/.net legacy UUID representation.

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

See CSHARP_LEGACY for details.

在 3.11 版本加入.

JAVA_LEGACY = 5#

The Java legacy UUID representation.

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

See JAVA_LEGACY for details.

在 3.11 版本加入.

PYTHON_LEGACY = 3#

The Python legacy UUID representation.

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

See PYTHON_LEGACY for details.

在 3.11 版本加入.

STANDARD = 4#

The standard UUID representation.

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

See STANDARD for details.

在 3.11 版本加入.

UNSPECIFIED = 0#

An unspecified UUID representation.

When configured, uuid.UUID instances will not be automatically encoded to or decoded from Binary. When encoding a uuid.UUID instance, an error will be raised. To encode a uuid.UUID instance with this configuration, it must be wrapped in the Binary class by the application code. When decoding a BSON binary field with a UUID subtype, a Binary instance will be returned instead of a uuid.UUID instance.

See UNSPECIFIED for details.

在 3.11 版本加入.

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

基类:bytes

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 bytes or subtype is not an instance of int. Raises ValueError if subtype is not in [0, 256).

备注

Instances of Binary with subtype 0 will be decoded directly to bytes.

Parameters:
  • data: the binary data to represent. Can be any bytes-like type that implements the buffer protocol.

  • subtype (optional): the binary subtype to use

在 3.9 版本发生变更: Support any bytes-like type that implements the buffer protocol.

as_uuid(uuid_representation: int = 4) UUID#

Create a Python UUID from this BSON Binary object.

Decodes this binary object as a native uuid.UUID instance with the provided uuid_representation.

Raises ValueError if this Binary instance does not contain a UUID.

Parameters:

在 3.11 版本加入.

classmethod from_uuid(uuid: UUID, uuid_representation: int = 4) Binary#

Create a BSON Binary object from a Python UUID.

Creates a Binary object from a uuid.UUID instance. Assumes that the native uuid.UUID instance uses the byte-order implied by the provided uuid_representation.

Raises TypeError if uuid is not an instance of UUID.

Parameters:

在 3.11 版本加入.

property subtype: int#

Subtype of this binary data.