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

DEPRECATED: Manipulators that can edit SON objects as they enter and exita database.

The SONManipulator API has limitations as atechnique for transforming your data. Instead, it is more flexible andstraightforward to transform outgoing documents in your own code before passingthem to PyMongo, and transform incoming documents after receiving them fromPyMongo. SON Manipulators will be removed from PyMongo in 4.0.

PyMongo does not apply SON manipulators to documents passed tothe modern methods bulk_write(),insert_one(),insert_many(),update_one(), orupdate_many(). SON manipulators arenot applied to documents returned by the modern methodsfind_one_and_delete(),find_one_and_replace(), andfind_one_and_update().

  • class pymongo.sonmanipulator.AutoReference(_db)
  • Transparently reference and de-reference already saved embedded objects.

This manipulator should probably only be used when the NamespaceInjector isalso being used, otherwise it doesn’t make too much sense - documents canonly 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 fixthis we’ll need to add a DatabaseInjector that adds _db and then makeuse of the optional database support for DBRefs.

  • transformincoming(_son, collection)
  • Replace embedded documents with DBRefs.

  • transformoutgoing(_son, collection)

  • Replace DBRefs with embedded documents.

  • will_copy()

  • 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.

    • transformincoming(_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.

Changed in version 2.7: ObjectIdInjector is no longer used by PyMongo, but remains in thismodule for backwards compatibility.

  • transformincoming(_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.

    • transformincoming(_son, collection)
    • Move _id to the front if it’s there.

    • will_copy()

    • 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.

  • transformincoming(_son, collection)
  • Manipulate an incoming SON object.

Parameters:

  1. - _son_: the SON object to be inserted into the database
  2. - _collection_: the collection the object is being inserted into
  • transformoutgoing(_son, collection)
  • Manipulate an outgoing SON object.

Parameters:

  1. - _son_: the SON object being retrieved from the database
  2. - _collection_: the collection this object was stored in
  • will_copy()
  • Will this SON manipulator make a copy of the incoming document?

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