Video coming soon…

🍃 Setup MongoDB — Document Database

Deploy MongoDB document database on Ubuntu using Docker — flexible schema, horizontal scaling, ideal for Node.js and Python apps.

⚠️ This script is provided for demo and testing purposes only. Not intended for production use.

📦 Resources & Setup Scripts

Grab the automated bash script from GitHub to follow along with the video.

Automated install script — one command sets everything up.
View on GitHub

Quick Install:

wget https://raw.githubusercontent.com/mhmdali94/Docker/main/databases/mongodb/mongodb-ubuntu.sh
chmod +x mongodb-ubuntu.sh
sudo bash mongodb-ubuntu.sh

Tutorial Steps

1 Download the Script

wget https://raw.githubusercontent.com/mhmdali94/Docker/main/databases/mongodb/mongodb-ubuntu.sh

2 Make it Executable

chmod +x mongodb-ubuntu.sh

3 Run the Installer

The script installs Docker if needed, then deploys MongoDB with authentication enabled.

sudo bash mongodb-ubuntu.sh

4 Connect to MongoDB

Connect via mongosh or MongoDB Compass:

docker exec -it mongodb mongosh -u admin -p

5 Create Database and Collection

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()

Ports Used

PortPurpose
27017MongoDB — internal only, never expose directly

Overview

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.

Why Use It

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.

When You Need It

    Who Should Use It

      Real Use Cases

        Main Features

          How to Use After Installation

            Security Best Practices

              Ports and Firewall Notes

              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.

              Backup and Maintenance

                Common Mistakes

                  Troubleshooting

                    Alternatives

                    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).

                    When Not to Use It

                    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 Professional Help

                    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.

                      Contact Us

                      Frequently Asked Questions

                      Do I need to define a schema before inserting data?

                      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.

                      How do I connect MongoDB Compass to my self-hosted instance?

                      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.

                      Does MongoDB support ACID transactions?

                      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.

                      What is the aggregation pipeline?

                      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.

                      Can MongoDB scale horizontally?

                      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.

                      What is a TTL index?

                      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.

                      How do I back up MongoDB without downtime?

                      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.

                      How do I upgrade MongoDB?

                      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.