Deploy MongoDB document database on Ubuntu using Docker — flexible schema, horizontal scaling, ideal for Node.js and Python apps.
Grab the automated bash script from GitHub to follow along with the video.
wget https://raw.githubusercontent.com/mhmdali94/Docker/main/databases/mongodb/mongodb-ubuntu.sh
chmod +x mongodb-ubuntu.sh
sudo bash mongodb-ubuntu.sh
wget https://raw.githubusercontent.com/mhmdali94/Docker/main/databases/mongodb/mongodb-ubuntu.sh
chmod +x mongodb-ubuntu.sh
The script installs Docker if needed, then deploys MongoDB with authentication enabled.
sudo bash mongodb-ubuntu.sh
Connect via mongosh or MongoDB Compass:
docker exec -it mongodb mongosh -u admin -p
Create your first database, collection, and insert a test document to verify everything works.
use myapp
db.createCollection("users")
db.users.insertOne({ name: "Test User", email: "test@example.com" })
db.users.find()
| Port | Purpose |
|---|---|
| 27017 | MongoDB — internal only, never expose directly |
MongoDB is the world's most popular NoSQL document database, storing data as flexible JSON-like documents instead of fixed relational rows. It excels at handling varied, hierarchical, or rapidly evolving data structures — making it the default choice for modern web and mobile application backends.
MongoDB excels where your data structure is flexible or evolving. Instead of rigid table schemas, you store JSON-like documents that can have different fields per record. It's the natural fit for Node.js (native JSON), Python (PyMongo), and apps where requirements change frequently. Horizontal scaling via sharding handles data volumes that would be impractical in a single relational DB.
Port 27017 for MongoDB. Keep it strictly internal — never expose to the internet. Your applications connect via the internal Docker network. If you need external access (e.g., for MongoDB Compass), use an SSH tunnel, never direct port exposure.
Direct alternatives: PostgreSQL with JSONB (SQL + JSON in one DB — often better for structured data that occasionally needs flexibility), CouchDB (pure document DB with built-in replication), FerretDB (MongoDB-compatible API on PostgreSQL). Cloud: MongoDB Atlas (managed, generous free tier).
Don't use MongoDB if your data is relational (orders → customers → products) with complex joins — PostgreSQL handles this far better. And if ACID transactions are critical to your data model, PostgreSQL's transaction model is more mature. MongoDB is best when documents are self-contained and schema flexibility is a real requirement.
PrismaTechWork provides end-to-end infrastructure services — from initial deployment and security hardening to ongoing monitoring, automated backups, and dedicated support. Whether you need a single-server setup or a multi-site network, our team ensures your infrastructure is built right, secured properly, and maintained reliably.
No — MongoDB is schemaless by default. You insert documents and collections are created automatically. However, you can enforce a schema using MongoDB's JSON Schema Validation (`db.createCollection` with a validator) for production data integrity.
Use an SSH tunnel: `ssh -L 27017:localhost:27017 user@your-server`. Then connect Compass to `mongodb://user:pass@localhost:27017`. Never open port 27017 to the internet directly.
Yes — since MongoDB 4.0, multi-document ACID transactions are supported on replica sets, and since 4.2 on sharded clusters. Single-document operations were always atomic. For most apps, single-document atomicity is sufficient.
The aggregation pipeline processes documents through stages (like Unix pipes): `$match` → filter, `$group` → aggregate, `$project` → reshape, `$sort` → order. It's MongoDB's equivalent of SQL's `GROUP BY`, `JOIN`, and `HAVING` — very powerful for analytics queries.
Yes — via sharding, MongoDB distributes data across multiple nodes using a shard key. This enables horizontal scaling for very large datasets. For most self-hosted use cases (up to tens of GB), a single node or replica set is sufficient.
A TTL (Time-To-Live) index automatically deletes documents after a specified time: `db.logs.createIndex({createdAt: 1}, {expireAfterSeconds: 2592000})`. Documents older than 30 days are deleted automatically — ideal for session stores, logs, and temporary data.
Use `mongodump` (logical backup, consistent but slow for large datasets) or a filesystem snapshot of the data volume. With a replica set, run `mongodump` against a secondary node to avoid impacting the primary.
MongoDB requires sequential major version upgrades (4.4 → 5.0 → 6.0 → 7.0 — can't skip). Run `docker compose pull && docker compose up -d` for minor versions. For major versions, follow the official upgrade guide and set `featureCompatibilityVersion` at each step.