nanomongo.document — BaseDocument¶
BaseDocument¶
-
class
nanomongo.document.BaseDocument(*args, **kwargs)[source]¶ BaseDocument class. Subclasses should be used. See
__init__()
-
BaseDocument.__init__(*args, **kwargs)[source]¶ Inits the document with given data and validates the fields (field validation bad idea during init?). If you define
__init__method for your document class, make sure to call thisclass MyDoc(BaseDocument, dot_notation=True): foo = Field(str) bar = Field(int, required=False) def __init__(self, *args, **kwargs): super(MyDoc, self).__init__(*args, **kwargs) # do other stuff
-
classmethod
BaseDocument.register(client=None, db=None, collection=None)[source]¶ Register this document. Sets client, database, collection information, builds (ensure) indexes and sets SON manipulator
-
classmethod
BaseDocument.find(*args, **kwargs)[source]¶ pymongo.Collection().findwrapper for this document
-
classmethod
BaseDocument.find_one(*args, **kwargs)[source]¶ pymongo.Collection().find_onewrapper for this document
-
BaseDocument.validate()[source]¶ Override this to add extra document validation, will be called at the end of
validate_all()
-
BaseDocument.validate_all()[source]¶ Check against extra fields, run field validators and user-defined
validate()
-
BaseDocument.validate_diff()[source]¶ Check correctness of diffs before partial update, also run user-defined
validate()
-
BaseDocument.run_auto_updates()[source]¶ Runs functions in
nanomongo.transformslike auto_update stuff beforeinsert()save()
-
BaseDocument.insert(**kwargs)[source]¶ Insert document into database, return _id. Runs
run_auto_updates()andvalidate_all()
-
BaseDocument.save(**kwargs)[source]¶ Saves document. This method only does partial updates and no inserts. Runs
run_auto_updates()andvalidate_all()prior to save. ReturnsCollection.update()response
-
BaseDocument.addToSet(field, value)[source]¶ MongoDB
Collection.update()$addToSet functionality. This sets the value accordingly and records the change in__nanodiff__to be saved withsave().# MongoDB style dot notation can be used to add to lists # in embedded documents doc = Doc(foo=[], bar={}) doc.addToSet('foo', new_value) doc.addToSet('bar.sub_field', new_value)
Contrary to how $set has no effect under __setitem__ (see
RecordingDict.__setitem__) when the new value is equal to the current; $addToSet explicitly adds the call to__nanodiff__so it will be sent to the database whensave()is called.
-
BaseDocument.get_dbref()[source]¶ create a
bson.DBRefinstance for thisBaseDocumentinstance