R
Rishtaara
MongoDB Fundamentals
Lesson 39 of 40Article14 min

Connect Django to MongoDB

Django's ORM is built for relational databases (PostgreSQL, MySQL). MongoDB is document-oriented, so there is no official Django-MongoDB backend. Python developers use bridges like mongoengine or Djongo, or skip the ORM and use pymongo directly.

Django and MongoDB — the landscape

Django's ORM is built for relational databases (PostgreSQL, MySQL). MongoDB is document-oriented, so there is no official Django-MongoDB backend. Python developers use bridges like mongoengine or Djongo, or skip the ORM and use pymongo directly.

Real-life example: Connecting Django to MongoDB is like using a universal power adapter abroad — it works, but native plugs (PostgreSQL + Django ORM) fit more naturally.

  • mongoengine — standalone ODM, Django-like models but separate from Django ORM. Most popular for Django + MongoDB projects.
  • Djongo — translates Django ORM queries to MongoDB. Convenient but less maintained; check compatibility with your Django version.
  • pymongo — official driver; use in Django views/services when you want full control without an ODM layer.
  • For new Django projects needing MongoDB, many teams run Django with PostgreSQL for auth/admin and a separate Node/Python service for MongoDB-heavy features.

mongoengine connection sketch

settings.py — connect with mongoengine
# pip install mongoengine django
import os
import mongoengine

MONGODB_URI = os.environ.get(
    "MONGODB_URI",
    "mongodb://127.0.0.1:27017/rishtaara"
)

def connect_mongodb():
    mongoengine.connect(
        host=MONGODB_URI,
        alias="default",
    )

# Call in AppConfig.ready() or wsgi.py startup
connect_mongodb()
Document model and CRUD
from mongoengine import (
    Document, StringField, IntField, ListField,
    EmailField, DateTimeField,
)
from datetime import datetime

class Student(Document):
    meta = {"collection": "students"}
    name = StringField(required=True, max_length=120)
    email = EmailField(required=True, unique=True)
    grade = IntField(min_value=1, max_value=12)
    subjects = ListField(StringField())
    created_at = DateTimeField(default=datetime.utcnow)

# Create
student = Student(name="Asha", email="asha@school.edu", grade=10, subjects=["Math"])
student.save()

# Read
students = Student.objects(grade__gte=10).order_by("name")[:20]
one = Student.objects(email="asha@school.edu").first()

# Update
Student.objects(email="asha@school.edu").update(add_to_set__subjects="Physics")

# Delete
Student.objects(email="asha@school.edu").delete()

When Django + MongoDB makes sense vs PostgreSQL

Choose Django + MongoDB when you already have MongoDB data (IoT events, content documents, activity feeds) and want Python admin tooling or REST APIs alongside it. mongoengine models work well for document-shaped domains.

Choose Django + PostgreSQL when you need Django Admin on relational data, complex joins, foreign keys, and mature migration tooling (django migrations). Most Django tutorials and packages assume SQL.

Real-life example: A CMS with flexible article JSON bodies fits MongoDB. An e-commerce checkout with inventory locks and refunds fits PostgreSQL (or MongoDB with careful transaction design).

  • Django Admin does not work out-of-the-box with mongoengine documents — build custom admin or use PostgreSQL for admin-managed entities.
  • Use PostgreSQL for users, billing, and permissions; MongoDB for logs, analytics events, and user-generated content if you need both.
  • Atlas Data API or a thin FastAPI/Express layer often beats forcing Django ORM patterns onto MongoDB.
Tip: On Rishtaara, the MongoDB course focuses on Node.js + Mongoose for app integration. Use this lesson as a map for Python teams — mongoengine docs are the next stop.