nanomongo.document

class nanomongo.document.BaseDocument(*args, **kwargs)[source]

BaseDocument class. Subclasses should be used. See __init__()

add_to_set(field, value)[source]

Explicitly defined $addToSet functionality. This sets/updates the field value accordingly and records the change to be saved with save().

# MongoDB style dot notation can be used to add to lists
# in embedded documents
doc = Doc(foo=[], bar={})
doc.add_to_set('foo', new_value)

Contrary to how $set ing the same value has no effect under __setitem__ (see .util.RecordingDict.__setitem__()) when the new value is equal to the current, this explicitly records the change so it will be sent to the database when save() is called.

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

pymongo.Collection().find wrapper for this document

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

pymongo.Collection().find_one wrapper for this document

classmethod get_collection()[source]

Returns collection as set in nanomongo

get_dbref()[source]

Return a bson.DBRef instance for this BaseDocument instance

insert(**kwargs)[source]

Runs auto updates, validates the document, and inserts into database. Returns pymongo.results.InsertOneResult.

classmethod register(client=None, db=None, collection=None)[source]

Register this document. Sets client, database, collection information, creates indexes and sets SON manipulator

run_auto_updates()[source]

Runs auto_update functions in .nanomongo.transforms.

save(**kwargs)[source]

Runs auto updates, validates the document, and saves the changes into database. Returns pymongo.results.UpdateResult.

validate()[source]

Override this to add extra document validation. It will be called during insert() and save() before the database operation.

validate_all()[source]

Check correctness of the document before insert(). Ensure that

  • no extra (undefined) fields are present
  • field values are of correct data type
  • required fields are present
validate_diff()[source]

Check correctness of diffs (ie. $set and $unset) before save(). Ensure that

  • no extra (undefined) fields are present for either set or unset
  • field values are of correct data type
  • required fields are not unset