bson – Tools for working with BSON in Python

Tools for dealing with Mongo’s BSON data representation.

Generally not needed to be used by application developers.

class pymongo.bson.BSON

Bases: str

BSON data.

Represents binary data storable in and retrievable from Mongo.

classmethod from_dict(dct, check_keys=False)

Create a new BSON instance from a mapping type (like dict).

Raises TypeError if dct is not a mapping type, or contains keys that are not instances of basestring. Raises InvalidDocument if dct cannot be converted to BSON.

Parameters:
  • dct: mapping type representing a document
  • check_keys (optional): check if keys start with ‘$’ or contain ‘.’, raising InvalidName in either case
to_dict(as_class=<type 'dict'>)

Convert this BSON data to a mapping type.

The default type to use is dict. This can be replaced using the as_class parameter.

Parameters:
  • as_class (optional): the class to use for the resulting document

New in version 1.7: the as_class parameter

pymongo.bson.is_valid(bson)

Check that the given string represents valid BSON data.

Raises TypeError if bson is not an instance of str. Returns True if bson is valid BSON, False otherwise.

Parameters:
  • bson: the data to be validated

Previous topic

objectid – Tools for working with MongoDB ObjectIds

Next topic

message – Tools for creating messages to be sent to MongoDB

This Page