Indexing

You can create indexes for each collection using Index class. Its fairy easy but you should be familiar with indexing in databases. bad index could lead to poor performance.

class djamo.index.Index(keys, cache_time=300, **kwargs)

This class represent a Mongodb collection Index. You must using it via :py:attribute: ~djamo.collection.BaseCollection.indexes for example:

class Students(Collection):

    indexes = [
        Index("name", unique=True),
        Index(["age", "uid"]),
    ]

Note

You can use Index separatly by using its ensure method.

All optional index creation paramaters should be passed as keyword arguments to this method. Valid options include:

  • name: custom name to use for this index - if none is given,

    a name will be generated

  • unique: should this index guarantee uniqueness?

  • dropDups or drop_dups: should we drop duplicates

  • background: if this index should be created in the background

  • bucketSize or bucket_size: for use with geoHaystack indexes.

    Number of documents to group together within a certain proximity to a given longitude and latitude.

  • min: minimum value for keys in a GEO2D index

  • max: maximum value for keys in a GEO2D index

  • expireAfterSeconds: <int> Used to create an expiring (TTL) collection.

    MongoDB will automatically delete documents from this collection after <int> seconds. The indexed field must be a UTC datetime or the data will not expire.

Parameters:
  • keys – a single key or a list of (key, direction) pairs specifying the index to create
  • cache_time – (optional): time window (in seconds) during which this index will be recognized by subsequent calls to ensure_index - see documentation for ensure_index for details
  • **kwargs

    (optional): any additional index creation options (see the above list) should be passed as keyword arguments.

ensure(self, collection)

Create the index if it does not exists.

Parameters:collection – Collection object to create the index for.

This Page