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


Override this to add extra document validation, will be called at the end of validate_all()


Check against extra fields, run field validators and user-defined validate()


Check correctness of diffs before partial update, also run user-defined validate()


Runs functions in nanomongo.transforms like auto_update stuff before insert() save()


Insert document into database, return _id. Runs run_auto_updates() and validate_all()**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.


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