Skip to main content

Django backend for MongoDB

Django backend for MongoDB.

Supports:

  • Column mappings to MongoDB documents
  • Single table (collection) inheritance and single table OneToOne relationships
  • Filters (filter/exclude)

Setup / Configuration

Not supported as primary database, as Django contrib apps rely on Integer primary keys in built in migrations (and because it is a use case that is not a priority at the moment).

# settings.py
DATABASES = {
    # or any other primary databse
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    },
    # mongodb database, Client constructor options are passe in 'CLIENT', the database name in 'NAME'
    "mongodb": {
        "ENGINE": "django_mongodb",
        "NAME": "django_mongodb",
        "CONN_MAX_AGE": 120,
        "CLIENT": {
            "host": os.environ.get("MONGODB_URL"),
        },
    },
}
# A database is required
DATABASE_ROUTERS = ["testproject.router.DatabaseRouter"]

Using the database in models requires a DatabaseRouter, which could look like this

class DatabaseRouter:
    def db_for_read(self, model, **hints):
        if model._meta.app_label == "mymongoapp":
            return "default"
        return "default"

    def db_for_write(self, model, **hints):
        if model._meta.app_label == "mymongoapp":
            return "default"
        return "default"

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == obj2._meta.app_label:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label == "mymongoapp":
            # we are disabling migrations, as MongoDB is schema-less. Alerts, such as renaming fields, etc. are not supported
            return False
        return None

Finally we are going to change the default primary key of the app using MongoDB (if that is the case, otherwise add ObjectIdAutoField to the models, where you need it).

# apps.py
class TestappConfig(AppConfig):
    default_auto_field = "django_mongodb.models.ObjectIdAutoField"
    name = "mymongoapp"

Defining Models

A simple model, in an app, which has ObjectIdAutoField as default_auto_field

class MyModel(models.Model):
    json_field = JSONField()
    name = models.CharField(max_length=100)
    datetime_field = models.DateTimeField(auto_now_add=True)
    time_field = models.TimeField(auto_now_add=True)
    date_field = models.DateField(auto_now_add=True)

Single table inheritance

class SameTableChild(MyModel):
    my_model_ptr = models.OneToOneField(
        MyModel,
        on_delete=models.CASCADE,
        parent_link=True,
        related_name="same_table_child",
        # pointer to the primary key of the parent model
        db_column="_id",
    )
    extended = models.CharField(max_length=100)

    class Meta:
        # We are using the parent collection as db_table
        db_table = "mymongoapp_mymodel"

Single table OneToOne relationships

class SameTableOneToOne(models.Model):
    dummy_model = models.OneToOneField(
        MyModel,
        primary_key=True,
        on_delete=models.CASCADE,
        related_name="extends",
        db_column="_id",
    )
    extra = models.CharField(max_length=100)

    class Meta:
        # we are using the same collection to persist one-to-one relationships
        db_table = "mymongoapp_mymodel"

Querying

# get all objects
MyModel.objects.all()

# get all objects, which have a name in list ["foo", "bar"]
MyModel.objects.filter(name_in=["foo", "bar"])

# select related with single table inheritance and one to one relationships
MyModel.objects.select_related("same_table_child", "extends").all()

# simple aggregations
MyModel.objects.filter(name_in=["foo", "bar"]).count()

# raw mongo filter
MyModel.objects.filter(RawMongoDBQuery({"name": "1"})).delete()

Search

Using the prefer_search() extension of MongoQueryset, we can use the $search operator of MongoDB to query, if we have search indexes configured on the model.

MyModel.objects.prefer_search().filter(name="foo").all()

PostgreSQL search vectors map down to MongoDB search indexes, so we can use the same syntax as with PostgreSQL.

class MyModel(models.Model):
    name = models.CharField(max_length=100)
MyModel.objects.annotate(search=SearchVector('name')).filter(search=SearchQuery('foo')).all()

Raw Queries

with connections["mongodb"].cursor() as cursor:
    doc = cursor.collections["my_collection"].find_one()
    assert isinstance(doc["_id"], ObjectId)

Metadata

Release files for django-mongo-backend 0.29

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-mongo-backend 0.29
File Size Uploaded
django_mongo_backend-0.29.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mongo-backend 0.29
File Interpreter ABI Platform
django_mongo_backend-0.29-py3-none-any.whl Python 3 none any Details

Total release size: 33.1 kB

Release files / django_mongo_backend-0.29.tar.gz

Download URL django_mongo_backend-0.29.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5b9c472d209cf48d79948946e193d9026f0a54dfeb9a8ab77b168a67f43a4177
BLAKE2b-256 checksum
How to use checksums
780d03f6ae95363e00a935bed3ea1c113a9fc643988068f526322f1bfbad6917
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

Release files / django_mongo_backend-0.29-py3-none-any.whl

Download URL django_mongo_backend-0.29-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37b66bd3ca0d689d2343217778462de2d06fcdd9754fb5106913e5b9d383cca2
BLAKE2b-256 checksum
How to use checksums
aca3329ac205f9d4c35a47b88430e0d5be14ddd4bd7ee5b5333f6c6ed048b0ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

Release history Release notifications | RSS feed

This release

0.29 This release

2 release files

0.27.8

2 release files

0.27.7

2 release files

0.27.6

2 release files

0.27.5

2 release files

0.27.4

2 release files

0.27.3

2 release files

0.27.1

2 release files

0.27

2 release files

0.26

2 release files

0.25.3

2 release files

0.25

2 release files

0.24.1

2 release files

0.24

2 release files

0.23

2 release files

0.22

2 release files

0.21

2 release files

0.20

2 release files

0.18

2 release files

0.17

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page