son_manipulator – Manipulators that can edit SON documents as they are saved or retrieved

Manipulators that can edit SON objects as they enter and exit a database.

New manipulators should be defined as subclasses of SONManipulator and can be installed on a database by calling pymongo.database.Database.add_son_manipulator.

class pymongo.son_manipulator.AutoReference(db)

Transparently reference and de-reference already saved embedded objects.

This manipulator should probably only be used when the NamespaceInjector is also being used, otherwise it doesn’t make too much sense - documents can only be auto-referenced if they have an _ns field.

NOTE: this will behave poorly if you have a circular reference.

TODO: this only works for documents that are in the same database. To fix this we’ll need to add a DatabaseInjector that adds _db and then make use of the optional database support for DBRefs.

transform_incoming(son, collection)

Replace embedded documents with DBRefs.

transform_outgoing(son, collection)

Replace DBRefs with embedded documents.


We need to copy so the user’s document doesn’t get transformed refs.

class pymongo.son_manipulator.NamespaceInjector

A son manipulator that adds the _ns field.

transform_incoming(son, collection)

Add the _ns field to the incoming object

class pymongo.son_manipulator.ObjectIdInjector

A son manipulator that adds the _id field if it is missing.

transform_incoming(son, collection)

Add an _id field if it is missing.

class pymongo.son_manipulator.ObjectIdShuffler

A son manipulator that moves _id to the first position.

transform_incoming(son, collection)

Move _id to the front if it’s there.


We need to copy to be sure that we are dealing with SON, not a dict.

class pymongo.son_manipulator.SONManipulator

A base son manipulator.

This manipulator just saves and restores objects without changing them.

transform_incoming(son, collection)

Manipulate an incoming SON object.

Parameters :
  • son: the SON object to be inserted into the database
  • collection: the collection the object is being inserted into
transform_outgoing(son, collection)

Manipulate an outgoing SON object.

Parameters :
  • son: the SON object being retrieved from the database
  • collection: the collection this object was stored in

Will this SON manipulator make a copy of the incoming document?

Derived classes that do need to make a copy should override this method, returning True instead of False. All non-copying manipulators will be applied first (so that the user’s document will be updated appropriately), followed by copying manipulators.

Previous topic

replica_set_connection – Tools for connecting to a MongoDB replica set

Next topic

cursor_manager – Managers to handle when cursors are killed after being closed

This Page