MongoDB

MongoDB

Document database with flexible schema design

Features

  • Flexible document model (JSON/BSON)
  • Horizontal scaling with sharding
  • Aggregation pipeline for complex queries
  • Change streams for real-time data

Pros

  • Schema flexibility accelerates early development
  • Natural fit for JavaScript/TypeScript applications
  • Built-in horizontal scaling and replication

Cons

  • No ACID transactions across shards (limited multi-doc transactions)
  • Schema flexibility can lead to data inconsistency
  • Higher storage usage than relational databases

Overview

MongoDB is a document-oriented NoSQL database that stores data in flexible, JSON-like documents (BSON). Unlike relational databases with fixed schemas, MongoDB allows different documents in the same collection to have different structures, making it easy to evolve your data model over time.

MongoDB excels at handling large volumes of unstructured or semi-structured data. Its aggregation pipeline provides powerful data processing capabilities, and its built-in sharding enables horizontal scaling across multiple servers. Change streams provide real-time notifications when data changes.

When to Use

MongoDB is a good fit when your data model is document-oriented, when schema flexibility is important during rapid development, or when you need horizontal scaling for large datasets. For applications with complex relationships or requiring strict data integrity, PostgreSQL is often a better choice.

Getting Started

docker run -d --name mongo -p 27017:27017 mongo:7

npm install mongodb
import { MongoClient } from "mongodb";

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

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