---
title: DjongoNxt Indexes
permalink: /djongonxt-indexes/
---

{{ page.notice.not_standard }}


## Compound Index
```python
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](https://docs.djangoproject.com/en/dev/ref/models/indexes/#django.db.models.Index.fields).
`name` | `string` | The index name, if not provided will be autogenerated by MongoDB
`unique` | `boolean` | Used to create [unique indexes](https://docs.mongodb.com/manual/core/index-unique/)
`background` | `boolean` | Create the index in the background.
`partialFilterExpression` | `Q` | Used to create [partial indexes](https://docs.mongodb.com/manual/core/index-partial/). Similar to Django [Index condition](https://docs.djangoproject.com/en/dev/ref/models/indexes/#condition). Takes an instance of [Q](https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.Q) to build the filter expression.
`sparse` | `boolean` | Used to create [sparse indexes](https://docs.mongodb.com/manual/core/index-sparse/)
`collation` | `Collation` | Used to specify the [collation](https://docs.mongodb.com/manual/reference/collation/). Takes an instance of [collation](https://api.mongodb.com/python/current/api/pymongo/collation.html)

### Example

```python
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:

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

## Text Index

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

```python
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:

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

## Wildcard Index

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

```python
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:

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

## 2dsphere Index
```python
class TwoDSphereIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None,
    min=None,
    max=None)
```
### Example

```python
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:

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

## Hashed Index
```python
class HashedIndex(
    fields=(),
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None)
```
### Example

```python
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:

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

## TTL Index
```python
class TTLIndex(
    field=,
    name=None,
    unique=False,
    background=False,
    partialFilterExpression=None,
    sparse=False,
    collation=None,
    expireAfterSeconds=None)
```
### Example

```python
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:

```python
db.blog.create_index( { 'pub_date': 1 }, expireAfterSeconds=3600 )
```
<!--
## geoHaystack Indexes
-->


