-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 3.4
-
Component/s: None
-
None
-
Minor Change
Starting in 3.4 we can retrieve raw BSON documents from a cursor:
options = CodecOptions(document_class=RawBSONDocument) for doc in db.get_collection("collection", codec_options=options).find(): # doc is a RawBSONDocument. doc.raw
This is mainly intended for speed in libraries like Python-BSONJS that can decode raw BSON themselves. If the documents are small, however, each instantiation of a RawBSONDocument is much more costly than decoding the BSON to a dict would have been. The main penalty is from copying the CodecOptions class for each RawBSONDocument.
To avoid copying, we will have to require that the CodecOptions passed to a RawBSONDocument has document_class of RawBSONDocument. In normal usage like the example above, that is always true. But, there are direct usages of RawBSONDocument that will be prohibited now:
# Now prohibited, document_class defaults to dict.
document = RawBSONDocument(
bson_string,
codec_options=CodecOptions(uuid_representation=JAVA_LEGACY))
# Do this instead.
document = RawBSONDocument(
bson_string,
codec_options=CodecOptions(uuid_representation=JAVA_LEGACY,
document_class=RawBSONDocument))
- related to
-
PYTHON-1362 API for retrieving batches as raw document streams
- Closed