raw_bson – Tools for representing raw BSON documents.

Tools for representing raw BSON documents.

Inserting and Retrieving RawBSONDocuments

Example: Moving a document between different databases/collections

  1. >>> import bson
  2. >>> from pymongo import MongoClient
  3. >>> from bson.raw_bson import RawBSONDocument
  4. >>> client = MongoClient(document_class=RawBSONDocument)
  5. >>> client.drop_database('db')
  6. >>> client.drop_database('replica_db')
  7. >>> db = client.db
  8. >>> result = db.test.insert_many([{'a': 1},
  9. ... {'b': 1},
  10. ... {'c': 1},
  11. ... {'d': 1}])
  12. >>> replica_db = client.replica_db
  13. >>> for doc in db.test.find():
  14. ... print("raw document: %r" % (doc.raw,))
  15. ... result = replica_db.test.insert_one(doc)
  16. raw document: '...'
  17. raw document: '...'
  18. raw document: '...'
  19. raw document: '...'
  20. >>> for doc in replica_db.test.find(projection={'_id': 0}):
  21. ... print("decoded document: %r" % (bson.decode(doc.raw),))
  22. decoded document: {u'a': 1}
  23. decoded document: {u'b': 1}
  24. decoded document: {u'c': 1}
  25. decoded document: {u'd': 1}

For use cases like moving documents across different databases or writing binary blobs to disk, using raw BSON documents provides better speed and avoids the overhead of decoding or encoding BSON.

bson.raw_bson.DEFAULT_RAW_BSON_OPTIONS = CodecOptions(document_class=<class ‘bson.raw_bson.RawBSONDocument’>, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None))

The default CodecOptions for RawBSONDocument.

class bson.raw_bson.RawBSONDocument(bson_bytes, codec_options=None)

Create a new RawBSONDocument

RawBSONDocument is a representation of a BSON document that provides access to the underlying raw BSON bytes. Only when a field is accessed or modified within the document does RawBSONDocument decode its bytes.

RawBSONDocument implements the Mapping abstract base class from the standard library so it can be used like a read-only dict:

  1. >>> from bson import encode
  2. >>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'}))
  3. >>> raw_doc.raw
  4. b'...'
  5. >>> raw_doc['_id']
  6. 'my_doc'

Changed in version 3.8: RawBSONDocument now validates that the bson_bytes passed in represent a single bson document.

Changed in version 3.5: If a CodecOptions is passed in, its document_class must be RawBSONDocument.

  • items()

    Lazily decode and iterate elements in this document.

  • property raw

    The raw BSON bytes composing this document.