bson – BSON (Binary JSON) Encoding and Decoding¶
BSON (Binary JSON) encoding and decoding.
-
class
bson.BSON¶ BSON (Binary JSON) data.
-
decode(as_class=<type 'dict'>, tz_aware=False, uuid_subtype=3, compile_re=True)¶ Decode this BSON data.
The default type to use for the resultant document is
dict. Any other class that supports__setitem__()can be used instead by passing it as the as_class parameter.If tz_aware is
True(recommended), anydatetimeinstances returned will be timezone-aware, with their timezone set tobson.tz_util.utc. Otherwise (default), alldatetimeinstances will be naive (but contain UTC).Parameters: - as_class (optional): the class to use for the resulting document
- tz_aware (optional): if
True, return timezone-awaredatetimeinstances - compile_re (optional): if
False, don’t attempt to compile BSON regular expressions into Python regular expressions. Return instances ofRegexinstead. Can avoidInvalidBSONerrors when receiving Python-incompatible regular expressions, for example fromcurrentOp
Changed in version 2.7: Added
compile_reoption.New in version 1.9.
-
classmethod
encode(document, check_keys=False, uuid_subtype=3)¶ Encode a document to a new
BSONinstance.A document can be any mapping type (like
dict).Raises
TypeErrorif document is not a mapping type, or contains keys that are not instances ofbasestring(strin python 3). RaisesInvalidDocumentif document cannot be converted toBSON.Parameters: - document: mapping type representing a document
- check_keys (optional): check if keys start with ‘$’ or
contain ‘.’, raising
InvalidDocumentin either case
New in version 1.9.
-
-
bson.decode_all(data, as_class=<type 'dict'>, tz_aware=True, uuid_subtype=3, compile_re=True)¶ Decode BSON data to multiple documents.
data must be a string of concatenated, valid, BSON-encoded documents.
Parameters: - data: BSON data
- as_class (optional): the class to use for the resulting documents
- tz_aware (optional): if
True, return timezone-awaredatetimeinstances - compile_re (optional): if
False, don’t attempt to compile BSON regular expressions into Python regular expressions. Return instances ofRegexinstead. Can avoidInvalidBSONerrors when receiving Python-incompatible regular expressions, for example fromcurrentOp
Changed in version 2.7: Added compile_re option.
New in version 1.9.
-
bson.has_c()¶ Is the C extension installed?
New in version 1.9.
-
bson.has_uuid()¶ Is the uuid module available?
New in version 2.3.
-
bson.is_valid(bson)¶ Check that the given string represents valid
BSONdata.Raises
TypeErrorif bson is not an instance ofstr(bytesin python 3). ReturnsTrueif bson is validBSON,Falseotherwise.Parameters: - bson: the data to be validated
Sub-modules:
binary– Tools for representing binary data to be stored in MongoDBregex– Tools for representing MongoDB regular expressionscode– Tools for representing JavaScript codedbref– Tools for manipulating DBRefs (references to documents stored in MongoDB)errors– Exceptions raised by thebsonpackagejson_util– Tools for using Python’sjsonmodule with BSON documentsmax_key– Representation for the MongoDB internal MaxKey typemin_key– Representation for the MongoDB internal MinKey typeobjectid– Tools for working with MongoDB ObjectIdsson– Tools for working with SON, an ordered mappingtimestamp– Tools for representing MongoDB internal Timestampstz_util– Utilities for dealing with timezones in Python