Indexes

Compound Index

class CompoundIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None)
copy code

Arguments

ArgumentTypeDescription
fieldsTuple or ListA tuple of strings containing the fields to be indexed. Adding a '-' before the field name, builds the index in the descending order.
namestringThe index name, if not provided will be autogenerated by MongoDB
uniquebooleanUsed to create unique indexes
backgroundbooleanCreate the index in the background.
partialFilterExpressionQUsed to create partial indexes. Similar to Django Index condition. Takes an instance of Q to build the filter expression.
sparsebooleanUsed to create sparse indexes
collationCollationUsed to specify the collation. Takes an instance of collation

Example

from djongo.models.indexes import CompoundIndex
from djongo import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        indexes = [
            CompoundIndex(fields=['name', '-tagline'])
        ]
copy code

Generates the pymongo command:

db.blog.create_index( { 'name': 1, 'tagline': -1 } )
copy code

Text Index

class TextIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    weights=None, 
    default_language='english', 
    language_override=None, 
    textIndexVersion=None)
copy code

Example

from djongo.models.indexes import TextIndex
from djongo import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        indexes = [
            TextIndex(fields=['name', 'tagline'])
        ]
copy code

Generates the pymongo command:

db.blog.create_index( { 'name': 'text', 'tagline': 'text' } )
copy code

Wildcard Index

class WildcardIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None,
    wildcardProjection=None)
copy code

Example

from djongo.models.indexes import WildcardIndex
from djongo import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        abstract = True

class Entry(models.Model):
    blog = models.EmbeddedField(
        model_container=Blog,
    )
    class Meta:
        indexes = [
            WildcardIndex(fields=['blog'])
        ]
copy code

Generates the pymongo command:

db.entry.create_index( { 'blog.$**': 1 } )
copy code

2dsphere Index

class TwoDSphereIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None,
    min=None,
    max=None)
copy code

Example

from djongo.models.indexes import TwoDSphereIndex
from djongo import models

class Location(models.Model):
    type = models.CharField(max_length=100)
    coordinates = models.ArrayField()

    class Meta:
        abstract = True

class Entry(models.Model):
    loc = models.EmbeddedField(
        model_container=Location,
    )
    class Meta:
        indexes = [
            TwoDSphereIndex(fields=['loc'])
        ]
copy code

Generates the pymongo command:

db.entry.create_index( { 'loc' : '2dsphere' } )
copy code

Hashed Index

class HashedIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None)
copy code

Example

from djongo.models.indexes import HashedIndex
from djongo import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        indexes = [
            HashedIndex(fields=['name'])
        ]
copy code

Generates the pymongo command:

db.blog.create_index( { 'name': 'hashed' } )
copy code

TTL Index

class TTLIndex(
    field=,
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None,
    expireAfterSeconds=None)
copy code

Example

from djongo.models.indexes import TTLIndex
from djongo import models

class Blog(models.Model):
    pub_date = models.DateTimeField()

    class Meta:
        indexes = [
            TTLIndex(field='pub_date', expireAfterSeconds=3600)
        ]
copy code

Generates the pymongo command:

db.blog.create_index( { 'pub_date': 1 }, expireAfterSeconds=3600 )
copy code