7

Is there a way to tell pymongo to use a custom encoder to convert python objects to BSON?

Specifically I need to convert numpy arrays into BSON. I know I can manually ensure every numpy array gets converted to a native python array before sending it to pymongo. But this is repetitive and error-prone. I'd much rather have a way to set up my pymongo connection to do this automatically.

Leopd
  • 41,333
  • 31
  • 129
  • 167

1 Answers1

2

You need to write a SONManipulator. From the docs:

SONManipulator instances allow you to specify transformations to be applied automatically by PyMongo.

from pymongo.son_manipulator import SONManipulator

class Transform(SONManipulator):
  def transform_incoming(self, son, collection):
    for (key, value) in son.items():
      if isinstance(value, Custom):
        son[key] = encode_custom(value)
      elif isinstance(value, dict): # Make sure we recurse into sub-docs
        son[key] = self.transform_incoming(value, collection)
    return son
  def transform_outgoing(self, son, collection):
    for (key, value) in son.items():
      if isinstance(value, dict):
        if "_type" in value and value["_type"] == "custom":
          son[key] = decode_custom(value)
        else: # Again, make sure to recurse into sub-docs
          son[key] = self.transform_outgoing(value, collection)
    return son

then add it to your pymongo database object:

db.add_son_manipulator(Transform())

Note you don't have to add the _type field if you want to silently cast a numpy array to a python array.

muudscope
  • 6,780
  • 4
  • 21
  • 20
  • 3
    `son_manipulator` is currently deprecated. They'll be removed on v4.0. The official recomendation is to transform documents before passing them to pymongo, as stated in the [docs](https://api.mongodb.com/python/current/api/pymongo/son_manipulator.html) – el.atomo Oct 05 '17 at 15:22