---
name: mongodb-django
description: "Provisions a MongoDB database for a Django app with `npx extraorbital add mongodb` (no signup, no card) and wires it into Django with `pymongo`. Use when a Django project needs MongoDB, Mongo, a document database, MONGODB_URI or PyMongo, or when MONGODB_URI is missing or unset in .env."
---

# MongoDB in Django

<!-- Generated from ExtraOrbital's service registry by `pnpm skills:build` in platform/app. Edit the generator, not this file. -->

ExtraOrbital provisions a MongoDB database in one command and writes the credentials into the project's env file. For anything this recipe does not cover, use the `extraorbital` skill or https://extraorbital.dev/llms.txt.

## 1. Provision

```bash
npx extraorbital add mongodb
```

Run it from the project root. It is idempotent (a second run returns the same resource) and writes to `.env.local` if the project has one, otherwise `.env`, keeping that file out of git. Add `--json` to parse the result; credential values are redacted there, and you should never print them either.

| Variable | |
| --- | --- |
| `MONGODB_URI` | Carries the user, the password, TLS and the one database they reach |

Free on the Prototype plan: 512 MB, shared RAM and vCPU.

## 2. Install the client

```bash
pip install python-dotenv pymongo
```

## 3. Connect

Django reads no env file by itself: `settings.py` loads it with `python-dotenv`, below `BASE_DIR`.

**`yourproject/settings.py`**

```python
import os

from dotenv import load_dotenv

# ExtraOrbital writes .env.local when the project has one, else .env.
# load_dotenv never overrides a variable already set, so the first file wins.
load_dotenv(BASE_DIR / ".env.local")
load_dotenv(BASE_DIR / ".env")

MONGODB_URI = os.environ["MONGODB_URI"]
```

**`yourapp/mongodb.py`**

```python
from pymongo import MongoClient

from django.conf import settings

mongo = MongoClient(settings.MONGODB_URI)
# The database named in the connection string: the only one the credentials reach.
mongo_db = mongo.get_default_database()
```

## 4. Use it

**`yourapp/views.py`**

```python
from django.http import JsonResponse

from yourapp.mongodb import mongo_db

# yourapp/urls.py: path("api/mongodb/", views.check_mongodb)


def check_mongodb(request):
    mongo_db.events.insert_one({"type": "ping"})
    events = mongo_db.events.count_documents({})
    return JsonResponse({"events": events})
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
python manage.py runserver
curl http://localhost:8000/api/mongodb/
```

A JSON answer means the credentials, the client and the route all work. Delete the route afterwards if the app does not need it.

## Pitfalls

- Call `get_default_database()` with no name: the user in `MONGODB_URI` is scoped to the database in the string.
- Create the client once per process, as here; PyMongo pools connections inside it.
- `yourapp` and `yourproject` stand for the project's own app and settings package.
- Read credentials once in `settings.py` and import `django.conf.settings` elsewhere; `os.environ[...]` fails at startup when one is missing, which is the point.
- Exit code 3 means the account is past its free allowance: show the human the link the command printed and wait. Do not retry or work around it.

Docs: https://extraorbital.dev/docs/resources/mongo.md
