**DEPRECATED** - UUID wrapper to support working with UUIDs stored as
PYTHON_LEGACY.
.. note:: This class has been deprecated and will be removed in
PyMongo 4.0. Use :meth:`~bson.binary.Binary.from_uuid` and
:meth:`~bson.binary.Binary.as_uuid` with the appropriate
:class:`~bson.binary.UuidRepresentation` to handle legacy-formatted
UUIDs instead.::
from bson import Binary, UUIDLegacy, UuidRepresentation
import uuid
my_uuid = uuid.uuid4()
legacy_uuid = UUIDLegacy(my_uuid)
binary_uuid = Binary.from_uuid(
my_uuid, UuidRepresentation.PYTHON_LEGACY)
assert legacy_uuid == binary_uuid
assert legacy_uuid.uuid == binary_uuid.as_uuid(
UuidRepresentation.PYTHON_LEGACY)
.. doctest::
>>> import uuid
>>> from bson.binary import Binary, UUIDLegacy, STANDARD
>>> from bson.codec_options import CodecOptions
>>> my_uuid = uuid.uuid4()
>>> coll = db.get_collection('test',
... CodecOptions(uuid_representation=STANDARD))
>>> coll.insert_one({'uuid': Binary(my_uuid.bytes, 3)}).inserted_id
ObjectId('...')
>>> coll.count_documents({'uuid': my_uuid})
0
>>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)})
1
>>> coll.find({'uuid': UUIDLegacy(my_uuid)})[0]['uuid']
UUID('...')
>>>
>>> # Convert from subtype 3 to subtype 4
>>> doc = coll.find_one({'uuid': UUIDLegacy(my_uuid)})
>>> coll.replace_one({"_id": doc["_id"]}, doc).matched_count
1
>>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)})
0
>>> coll.count_documents({'uuid': {'$in': [UUIDLegacy(my_uuid), my_uuid]}})
1
>>> coll.find_one({'uuid': my_uuid})['uuid']
UUID('...')
Raises :exc:`TypeError` if `obj` is not an instance of :class:`~uuid.UUID`.
:Parameters:
- `obj`: An instance of :class:`~uuid.UUID`.
.. versionchanged:: 3.11
Deprecated. The same functionality can be replicated using the
:meth:`~Binary.from_uuid` and :meth:`~Binary.to_uuid` methods with
:data:`~UuidRepresentation.PYTHON_LEGACY`.
.. versionadded:: 2.1