nanomongo.documentBaseDocument

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 this

class 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.get_collection()[source]

Returns collection as set in nanomongo

classmethod BaseDocument.find(*args, **kwargs)[source]

pymongo.Collection().find wrapper for this document

classmethod BaseDocument.find_one(*args, **kwargs)[source]

pymongo.Collection().find_one wrapper 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.transforms like auto_update stuff before insert() save()

BaseDocument.insert(**kwargs)[source]

Insert document into database, return _id. Runs run_auto_updates() and validate_all()

BaseDocument.save(**kwargs)[source]

Saves document. This method only does partial updates and no inserts. Runs run_auto_updates() and validate_all() prior to save. Returns Collection.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 with save().

# 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 when save() is called.

BaseDocument.get_dbref()[source]

create a bson.DBRef instance for this BaseDocument instance

nanomongo.document.ref_getter_maker(field_name, document_class=None)[source]

create dereference methods for given field_name to be bound to Document instances