DjongoNxt Indexes

Note: Features under development in this section are not part of the standard djongo package. Visit the support page for more information.

Compound Index

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

Arguments

Argument Type Description
fields Tuple or List A tuple of strings containing the fields to be indexed. Adding a ‘-‘ before the field name, builds the index in the descending order.
name string The index name, if not provided will be autogenerated by MongoDB
unique boolean Used to create unique indexes
background boolean Create the index in the background.
partialFilterExpression Q Used to create partial indexes. Similar to Django Index condition. Takes an instance of Q to build the filter expression.
sparse boolean Used to create sparse indexes
collation Collation Used 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'])
        ]

Generates the pymongo command:

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

Text Index

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

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'])
        ]

Generates the pymongo command:

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

Wildcard Index

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

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'])
        ]

Generates the pymongo command:

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

2dsphere Index

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

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'])
        ]

Generates the pymongo command:

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

Hashed Index

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

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'])
        ]

Generates the pymongo command:

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

TTL Index

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

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)
        ]

Generates the pymongo command:

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