A beginner-friendly guide to the leading NoSQL document database โ from install to your first CRUD operations
MongoDB is a popular NoSQL document database. Instead of storing data in rows and columns like PostgreSQL, it stores data as documents โ self-contained JSON-like records grouped into collections. Each document can hold its own structure, which makes MongoDB flexible for data that changes shape.
| Scenario | Why MongoDB |
|---|---|
| Content & CMS | Blog posts and articles naturally vary in structure |
| IoT & sensor data | Flexible schemas ingest many device shapes |
| Catalogs | Products with optional attributes โ no sparse-column pain |
| Real-time analytics | Aggregation pipelines power instant dashboards |
Let's store the same little "product" two ways to see the thinking differ.
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT, price NUMERIC, color TEXT, size TEXT -- all rows share all columns ); INSERT INTO products (name, price, color, size) VALUES ('T-shirt', 20, 'blue', 'M');
db.products.insertOne({ name: "T-shirt", price: 20, details: { color: "blue", size: "M" } }); // another product can have totally different fields db.products.insertOne({ name: "Hat", price: 12 });
| Term | What it is |
|---|---|
| Database | A named container for collections |
| Collection | A group of documents (roughly a "table") |
| Document | A single JSON-like record (roughly a "row") |
| Field | A key/value pair inside a document (roughly a "column") |
_id | The unique primary key MongoDB adds automatically to every document |
| Embedded doc / array | Nested objects inside a document โ denormalized but fast |
| Aggregation pipeline | Chained stages ($match, $group) that transform data |
MongoDB community self-licence repo. Add the official package repository, then:
$ sudo apt update $ sudo apt install -y mongodb-org $ sudo systemctl enable --now mongod $ systemctl status mongod
Run the MSI installer from mongodb.com/try/download/community. Check Install MongoDB as a Service โ then MongoDB starts automatically.
> net start MongoDB
$ docker run -d --name mongo -p 27017:27017 mongo
Published to port 27017 โ the default Mongo server port.
$ mongosh // Create โ insert a document db.products.insertOne({ name: "Keyboard", price: 45 }); // Read โ find everything (pretty-printed) db.products.find().pretty() // Read with a filter (matches the field) db.products.find({ name: "Keyboard" }) // Update โ set a field db.products.updateOne( { name: "Keyboard" }, { $set: { price: 49 } } ); // Delete โ remove matches db.products.deleteOne({ name: "Keyboard" });
mongosh is the modern MongoDB shell โ type db to confirm you're connected.
| Command | What it does |
|---|---|
db.collection.insertOne({...}) | Insert one document |
db.collection.find({}) | Query documents |
db.collection.updateOne(filter, {$set:{...}}) | Update first matching doc |
db.collection.deleteOne(filter) | Delete first matching doc |
db.collection.aggregate([...]) | Chained transform pipeline |
show dbs | List databases |
use <db> | Switch/select a database |
show collections | List collections in current db |
mongodump / mongorestore for one-off copies; Atlas has built-in backups.
MongoDB trades rigid structure for flexibility โ when your data grows, your documents grow with it.
Install it, insert a document, run a query, and you've met the most popular NoSQL store.
Created by jcmatira