bson – BSON (Binary JSON) Encoding and Decoding
The mapping from Python types to BSON types is as follows:
Note that, when using Python 2.x, to save binary data it must be wrapped as an instance of bson.binary.Binary. Otherwise it will be saved as a BSON string and retrieved as unicode. Users of Python 3.x can use the Python bytes type.
A Python int will be saved as a BSON int32 or BSON int64 depending on its size. A BSON int32 will always decode to a Python int. A BSON int64 will always decode to a Int64. |
datetime.datetime instances will be rounded to the nearest millisecond when saved |
[3] | all datetime.datetime instances are treated as naive. clients should always use UTC. |
The bytes type from Python 3.x is encoded as BSON binary with subtype 0. In Python 3.x it will be decoded back to bytes. In Python 2.x it will be decoded to an instance of Binary with subtype 0. |
class bson.BSON
BSON (Binary JSON) data.
Warning
Using this class to encode and decode BSON adds a performance cost. For better performance use the module level functions and decode() instead.
decode
(codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))Decode this BSON data.
By default, returns a BSON document represented as a Python . To use a different
MutableMapping
class, configure a CodecOptions:Parameters: - codec_options (optional): An instance of .
Changed in version 3.0: Removed compile_re option: PyMongo now always represents BSON regular expressions as Regex objects. Use to attempt to convert from a BSON regular expression to a Python regular expression object.
Replaced as_class, tz_aware, and uuid_subtype options with codec_options.
Changed in version 2.7: Added compile_re option. If set to False, PyMongo represented BSON regular expressions as Regex objects instead of attempting to compile BSON regular expressions as Python native regular expressions, thus preventing errors for some incompatible patterns, see .
classmethod
encode
(document, check_keys=False, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))Encode a document to a new BSON instance.
A document can be any mapping type (like ).
Raises TypeError if document is not a mapping type, or contains keys that are not instances of
basestring
( in python 3). Raises InvalidDocument if document cannot be converted to .Parameters: - document: mapping type representing a document
- check_keys (optional): check if keys start with ‘$’ or contain ‘.’, raising InvalidDocument in either case
- codec_options (optional): An instance of .
Changed in version 3.0: Replaced uuid_subtype option with codec_options.
Decode BSON to a document.
By default, returns a BSON document represented as a Python dict. To use a different MutableMapping
class, configure a :
>>> import collections # From Python standard library.
>>> from bson.codec_options import CodecOptions
>>> data = bson.encode({'a': 1})
>>> decoded_doc = bson.decode(data)
<type 'dict'>
>>> options = CodecOptions(document_class=collections.OrderedDict)
>>> decoded_doc = bson.decode(data, codec_options=options)
<class 'collections.OrderedDict'>
New in version 3.9.
(data, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))
Decode BSON data to multiple documents.
data must be a bytes-like object implementing the buffer protocol that provides concatenated, valid, BSON-encoded documents.
Parameters: |
|
---|
Changed in version 3.9: Supports bytes-like objects that implement the buffer protocol.
Changed in version 3.0: Removed compile_re option: PyMongo now always represents BSON regular expressions as objects. Use try_compile() to attempt to convert from a BSON regular expression to a Python regular expression object.
Replaced as_class, tz_aware, and uuid_subtype options with codec_options.
Changed in version 2.7: Added compile_re option. If set to False, PyMongo represented BSON regular expressions as objects instead of attempting to compile BSON regular expressions as Python native regular expressions, thus preventing errors for some incompatible patterns, see PYTHON-500.
bson.decode_file_iter
(file_obj, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))
Decode bson data from a file to multiple documents as a generator.
Works similarly to the decode_all function, but reads from the file object in chunks and parses bson in chunks, yielding one document at a time.
Parameters: |
|
---|
Changed in version 3.0: Replaced as_class, tz_aware, and uuid_subtype options with codec_options.
New in version 2.8.
bson.decode_iter
(data, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))
Decode BSON data to multiple documents as a generator.
data must be a string of concatenated, valid, BSON-encoded documents.
Parameters: |
|
---|
Changed in version 3.0: Replaced as_class, tz_aware, and uuid_subtype options with codec_options.
New in version 2.8.
bson.encode
(document, check_keys=False, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.PYTHON_LEGACY, unicode_decode_error_handler=’strict’, tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None)))
Encode a document to BSON.
A document can be any mapping type (like ).
Raises TypeError if document is not a mapping type, or contains keys that are not instances of basestring
( in python 3). Raises InvalidDocument if document cannot be converted to .
New in version 3.9.
bson.gen_list_name
()
Generate “keys” for encoded lists in the sequence b”0”, b”1”, b”2”, …
The first 1000 keys are returned from a pre-built cache. All subsequent keys are generated on the fly.
bson.has_c
()
Is the C extension installed?
bson.is_valid
(bson)
Check that the given string represents valid BSON data.
Raises if bson is not an instance of str ( in python 3). Returns True
if bson is valid BSON, otherwise.
Parameters: |
|
---|
Sub-modules:
- codec_options – Tools for specifying BSON codec options
- decimal128 – Support for BSON Decimal128
- int64 – Tools for representing BSON int64
- max_key – Representation for the MongoDB internal MaxKey type
- objectid – Tools for working with MongoDB ObjectIds
- regex – Tools for representing MongoDB regular expressions
- timestamp – Tools for representing MongoDB internal Timestamps