πŸ“ Tutorials

What is MongoDB? A Simple Explanation for Developers


MongoDB is a document database. Instead of storing data in tables with rows and columns (like PostgreSQL), it stores data as JSON-like documents in collections.

// A MongoDB document
{
  _id: ObjectId("507f1f77bcf86cd799439011"),
  name: "Alice",
  email: "alice@example.com",
  address: {
    city: "Brussels",
    country: "Belgium"
  },
  tags: ["developer", "blogger"]
}

No rigid schema. Each document can have different fields. You can nest objects and arrays directly β€” no joins needed.

Documents vs. tables

PostgreSQL (relational):

users table:     | id | name  | email              |
                 | 1  | Alice | alice@example.com  |

addresses table: | id | user_id | city     | country |
                 | 1  | 1       | Brussels | Belgium |

-- Need a JOIN to get user + address

MongoDB (document):

// Everything in one document β€” no joins
{
  name: "Alice",
  email: "alice@example.com",
  address: { city: "Brussels", country: "Belgium" }
}

When to use MongoDB

Good fit:

  • Flexible or evolving schemas (startup MVPs, prototypes)
  • Data that’s naturally nested (user profiles, product catalogs, CMS content)
  • High write throughput
  • When you don’t need complex joins

Use PostgreSQL instead when:

  • Your data is highly relational (orders β†’ products β†’ categories)
  • You need complex queries with joins and aggregations
  • Data integrity is critical (financial data, inventory)
  • You want full SQL power

Using MongoDB

import { MongoClient } from 'mongodb';

const client = new MongoClient('mongodb://localhost:27017');
const db = client.db('myapp');

// Insert
await db.collection('users').insertOne({
  name: 'Alice',
  email: 'alice@example.com',
});

// Find
const user = await db.collection('users').findOne({ email: 'alice@example.com' });

// Update
await db.collection('users').updateOne(
  { email: 'alice@example.com' },
  { $set: { name: 'Alice Smith' } }
);

// Delete
await db.collection('users').deleteOne({ email: 'alice@example.com' });

Most Node.js developers use Mongoose (an ODM) for schema validation and a nicer API on top of MongoDB.

Where to host MongoDB

  • MongoDB Atlas β€” official managed service, generous free tier (512 MB)
  • Self-hosted β€” Docker or bare metal
  • AWS DocumentDB β€” MongoDB-compatible managed service

See also: What is PostgreSQL? | What is JSON?