Documents and Indexes

exception ommongo.document.BadIndexException
class ommongo.document.DictDoc

Adds a mapping interface to a document. Supports __getitem__ and __contains__. Both methods will only retrieve values assigned to a field, not methods or other attributes.

setdefault(name, value)

if the name is set, return its value. Otherwse set name to value and return value

class ommongo.document.Document(retrieved_fields=None, loading_from_db=False, **kwargs)
Parameters:
  • retrieved_fields – The names of the fields returned when loading a partial object. This argument should not be explicitly set by subclasses
  • **kwargs – The values for all of the fields in the document. Any additional fields will raise a ExtraValueException and any missing (but required) fields will raise a MissingValueException. Both types of exceptions are subclasses of DocumentException.
classmethod add_subclass(subclass)

Register a subclass of this class. Maps the subclass to the value of subclass.config_polymorphic_identity if available.

classmethod base_query(exclude_subclasses=False)

Return the base query for this kind of document. If this class is not polymorphic, the query is empty. If it is polymorphic then a filter is added to match only this class and its subclasses.

Parameters:exclude_subclasses – If this is true, only match the current class. If it is false, the default, also return subclasses of this class.
classmethod class_name()

Returns the name of the class. The name of the class is also the default collection name.

config_default_sort = None
config_extra_fields = 'error'
config_full_name = None
config_namespace = 'global'
config_polymorphic = None
config_polymorphic_collection = False
config_polymorphic_identity = None
classmethod get_collection_name()

Returns the collection name used by the class. If the config_collection_name attribute is set it is used, otherwise the name of the class is used.

get_dirty_ops(with_required=False)

Returns a dict with the update operations necessary to make the changes to this object to the database version. It is mainly used internally for update() but may be useful for diagnostic purposes as well.

Parameters:with_required – Also include any field which is required. This is useful if the method is being called for the purposes of an upsert where all required fields must always be sent.
get_extra_fields()

if Document.config_extra_fields is set to ‘ignore’, this method will return a dictionary of the fields which couldn’t be mapped to the document.

classmethod get_fields()

Returns a dict mapping the names of the fields in a document or subclass to the associated Field

get_index_score()

Get index scores from full-text search.

classmethod get_indexes()

Returns all of the Index instances for the current class.

classmethod get_subclass(obj)

Get the subclass to use when instantiating a polymorphic object. The default implementation looks at cls.config_polymorphic to get the name of an attribute. Subclasses automatically register their value for that attribute on creation via their config_polymorphic_identity field. This process is then repeated recursively until None is returned (indicating that the current class is the correct one)

This method can be overridden to allow any method you would like to use to select subclasses. It should return either the subclass to use or None, if the original class should be used.

has_id()
mongo_id
to_ref(db=None)
classmethod transform_incoming(obj, session)

Tranform the SON object into one which will be able to be unwrapped by this document class.

This method is designed for schema migration systems.

class ommongo.document.DocumentMeta
mro() → list

return a type’s method resolution order

class ommongo.document.Index

This class is used in the class definition of a Document to specify a single, possibly compound, index. pymongo’s ensure_index will be called on each index before a database operation is executed on the owner document class.

Example

>>> class Donor(Document):
...     name = StringField()
...     age = IntField(min_value=0)
...     blood_type = StringField()
...
...     i_name = Index().ascending('name')
...     type_age = Index().ascending('blood_type').descending('age')
ASCENDING = 1
DESCENDING = -1
ascending(name)

Add a descending index for name to this index.

Parameters:name – Name to be used in the index
descending(name)

Add a descending index for name to this index.

Parameters:name – Name to be used in the index
ensure(collection)

Call the pymongo method ensure_index on the passed collection.

Parameters:collection – the pymongo collection to ensure this index is on
expire(after)

Add an expire after option to the index

Param:after: Number of second before expiration
geo2d(name, min=None, max=None)

Create a 2d index. See: http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Parameters:
  • name – Name of the indexed column
  • min – minimum value for the index
  • max – minimum value for the index
geo_haystack(name, bucket_size)

Create a Haystack index. See: http://www.mongodb.org/display/DOCS/Geospatial+Haystack+Indexing

Parameters:
  • name – Name of the indexed column
  • bucket_size – Size of the haystack buckets (see mongo docs)
unique(drop_dups=False)

Make this index unique, optionally dropping duplicate entries.

Parameters:drop_dups – Drop duplicate objects while creating the unique index? Default to False
class ommongo.document.Value(field, document, from_db=False, extra=False, retrieved=True)
clear_dirty()
delete()

Document

class ommongo.document.Document(retrieved_fields=None, loading_from_db=False, **kwargs)
Parameters:
  • retrieved_fields – The names of the fields returned when loading a partial object. This argument should not be explicitly set by subclasses
  • **kwargs – The values for all of the fields in the document. Any additional fields will raise a ExtraValueException and any missing (but required) fields will raise a MissingValueException. Both types of exceptions are subclasses of DocumentException.
classmethod add_subclass(subclass)

Register a subclass of this class. Maps the subclass to the value of subclass.config_polymorphic_identity if available.

classmethod base_query(exclude_subclasses=False)

Return the base query for this kind of document. If this class is not polymorphic, the query is empty. If it is polymorphic then a filter is added to match only this class and its subclasses.

Parameters:exclude_subclasses – If this is true, only match the current class. If it is false, the default, also return subclasses of this class.
classmethod class_name()

Returns the name of the class. The name of the class is also the default collection name.

config_default_sort = None
config_extra_fields = 'error'
config_full_name = None
config_namespace = 'global'
config_polymorphic = None
config_polymorphic_collection = False
config_polymorphic_identity = None
classmethod get_collection_name()

Returns the collection name used by the class. If the config_collection_name attribute is set it is used, otherwise the name of the class is used.

get_dirty_ops(with_required=False)

Returns a dict with the update operations necessary to make the changes to this object to the database version. It is mainly used internally for update() but may be useful for diagnostic purposes as well.

Parameters:with_required – Also include any field which is required. This is useful if the method is being called for the purposes of an upsert where all required fields must always be sent.
get_extra_fields()

if Document.config_extra_fields is set to ‘ignore’, this method will return a dictionary of the fields which couldn’t be mapped to the document.

classmethod get_fields()

Returns a dict mapping the names of the fields in a document or subclass to the associated Field

get_index_score()

Get index scores from full-text search.

classmethod get_indexes()

Returns all of the Index instances for the current class.

classmethod get_subclass(obj)

Get the subclass to use when instantiating a polymorphic object. The default implementation looks at cls.config_polymorphic to get the name of an attribute. Subclasses automatically register their value for that attribute on creation via their config_polymorphic_identity field. This process is then repeated recursively until None is returned (indicating that the current class is the correct one)

This method can be overridden to allow any method you would like to use to select subclasses. It should return either the subclass to use or None, if the original class should be used.

has_id()
mongo_id
to_ref(db=None)
classmethod transform_incoming(obj, session)

Tranform the SON object into one which will be able to be unwrapped by this document class.

This method is designed for schema migration systems.

Index

class ommongo.document.Index

This class is used in the class definition of a Document to specify a single, possibly compound, index. pymongo’s ensure_index will be called on each index before a database operation is executed on the owner document class.

Example

>>> class Donor(Document):
...     name = StringField()
...     age = IntField(min_value=0)
...     blood_type = StringField()
...
...     i_name = Index().ascending('name')
...     type_age = Index().ascending('blood_type').descending('age')
ASCENDING = 1
DESCENDING = -1
ascending(name)

Add a descending index for name to this index.

Parameters:name – Name to be used in the index
descending(name)

Add a descending index for name to this index.

Parameters:name – Name to be used in the index
ensure(collection)

Call the pymongo method ensure_index on the passed collection.

Parameters:collection – the pymongo collection to ensure this index is on
expire(after)

Add an expire after option to the index

Param:after: Number of second before expiration
geo2d(name, min=None, max=None)

Create a 2d index. See: http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Parameters:
  • name – Name of the indexed column
  • min – minimum value for the index
  • max – minimum value for the index
geo_haystack(name, bucket_size)

Create a Haystack index. See: http://www.mongodb.org/display/DOCS/Geospatial+Haystack+Indexing

Parameters:
  • name – Name of the indexed column
  • bucket_size – Size of the haystack buckets (see mongo docs)
unique(drop_dups=False)

Make this index unique, optionally dropping duplicate entries.

Parameters:drop_dups – Drop duplicate objects while creating the unique index? Default to False