Local Development
Running the backend and database locally.
Overview
You can run the backend entirely on your machine using either a local MongoDB container or MongoDB Atlas. This page walks through both options and how to swap between them using environment variables defined in Configuration & Environment.
Quick start (Docker)
-
Install Docker Desktop.
-
Create
docker-compose.dev.yml:version: "3.9" services: mongo: image: mongodb/mongodb-community-server:7.0 ports: - "27017:27017" environment: MONGO_INITDB_DATABASE: tasks volumes: - mongo-data:/data/db volumes: mongo-data: {} -
Start MongoDB:
docker compose -f docker-compose.dev.yml up -d -
Copy
.env.development.exampleto.env.development.localand setMONGODB_URI=mongodb://localhost:27017/tasks. -
Run the Nest app:
pnpm dev(usesnext devwith docs; for the backend tutorial, runpnpm nest:startinside the sample project).
The backend reuses the same connection string when deployed if you point it to Atlas using stage-specific variables.
MongoDB Atlas option
If you prefer not to run Docker:
- Create a free Atlas cluster and database user.
- Whitelist your IP or enable access from everywhere for development.
- Copy the connection string and set it in
.env.development.localasMONGODB_URI_ATLAS. - Toggle the URI in your Nest configuration (see snippet below) to use Atlas instead of localhost.
const uri = process.env.USE_ATLAS === "true"
? process.env.MONGODB_URI_ATLAS
: process.env.MONGODB_URI;
MongooseModule.forRoot(uri, {
autoIndex: true,
serverSelectionTimeoutMS: 5000,
});Set USE_ATLAS=true in environments that should connect to Atlas. Keep local Docker as the default for onboarding consistency.
Seeding data
-
Use Nest command-line scripts or dedicated seeder modules to insert example documents (
tasks.seed.ts). -
Alternatively, run
mongosh:mongosh "mongodb://localhost:27017/tasks" --eval 'db.tasks.insertOne({ title: "First task", done: false })'
Running tests locally
- Unit tests:
pnpm test(configure in the tutorial project). - Integration tests can spin up an in-memory Mongo server using
mongodb-memory-serverfor deterministic runs.
Troubleshooting
| Symptom | Fix |
|---|---|
ECONNREFUSED when connecting | Ensure Docker container is running and port 27017 open. |
MongoServerError: user is not allowed | Check credentials; Atlas requires user with read/write role. |
| Slow cold start locally | Increase Lambda emulation memory or use pnpm start:dev which keeps Nest hot-reloaded. |
| Duplicate indexes after switching URIs | Run npm run schema:sync (if defined) or drop indexes via db.tasks.dropIndexes() in dev. |
See also Tutorial → Setup for step-by-step commands a junior engineer follows when onboarding.