ScriptsApr 11, 2026·1 min read

MongoDB — The Most Popular Document Database

MongoDB is a source-available, cross-platform document database. Stores data in flexible JSON-like documents, designed for horizontal scaling. Used by Adobe, eBay, Coinbase, and countless startups for their primary data store.

SC
Script Depot · Community
Quick Use

Use it first, then decide how deep to go

This block should tell both the user and the agent what to copy, install, and apply first.

# Install (macOS via Homebrew tap)
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community

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

# Connect
mongosh mongodb://localhost:27017
// Shell commands
use tokrepo

db.users.insertOne({
  name: "William",
  email: "w@tokrepo.com",
  roles: ["admin"],
  createdAt: new Date()
})

db.users.find({ roles: "admin" })

db.users.updateOne(
  { email: "w@tokrepo.com" },
  { $set: { lastLogin: new Date() }, $inc: { loginCount: 1 } }
)

db.users.aggregate([
  { $match: { roles: "admin" } },
  { $group: { _id: "$country", count: { $sum: 1 } } }
])

db.users.createIndex({ email: 1 }, { unique: true })
Intro

MongoDB is a source-available, cross-platform, document-oriented database. Instead of tables and rows, MongoDB stores JSON-like documents (BSON under the hood) in collections. Founded in 2007, now one of the most popular non-relational databases in the world.

What MongoDB Does

  • Document model — flexible schema, nested objects, arrays
  • CRUD API — insertOne, find, updateOne, deleteOne
  • Aggregation pipeline — stages for filter/group/project/lookup
  • Indexing — single, compound, text, geospatial, vector (Atlas)
  • Replication — replica sets for HA
  • Sharding — horizontal scale across shards
  • Transactions — multi-document ACID (v4+)
  • Change streams — subscribe to data changes
  • GridFS — large file storage
  • Time-series — optimized time-series collections

Architecture

Primary-secondary replica set for HA. Writes go to primary, replicated to secondaries via oplog. Sharded clusters split data by shard key across multiple replica sets. WiredTiger is the default storage engine since 3.2.

Self-Hosting

# Docker Compose replica set
version: "3"
services:
  mongo:
    image: mongo:7
    command: --replSet rs0 --bind_ip_all
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db
volumes:
  mongo-data:

Since v5, MongoDB Community Edition uses SSPL. Read the license before SaaS-wrapping.

Key Features

  • Document model (JSON-like)
  • Flexible schema
  • Aggregation pipeline
  • Full-text search (Atlas Search)
  • Vector search (Atlas, v7+)
  • Time-series collections
  • Change streams
  • Multi-document transactions
  • Geospatial queries
  • Horizontal scaling (sharding)

Comparison

Database Model Transactions Scale
MongoDB Document Multi-doc ACID (v4+) Sharding
PostgreSQL + JSONB Relational + JSON ACID Vertical + logical repl
Couchbase Document ACID Sharding
DynamoDB Key-value + doc ACID Managed
Firestore Document ACID Managed
Cassandra Wide column Eventual Multi-master

常见问题 FAQ

Q: MongoDB vs PostgreSQL? A: Postgres + JSONB 可以做 80% MongoDB 能做的事(加 ACID 和成熟 SQL 生态)。MongoDB 的优势是水平扩展、灵活 schema、社区 ORM(Mongoose)。

Q: SSPL 是什么意思? A: Source Available 但限制 SaaS 商用(如果你把 MongoDB 做成托管服务,必须开源整个服务栈)。自己用、嵌入应用完全 OK。

Q: 向量搜索? A: MongoDB Atlas 从 v7 开始内置向量搜索(RAG、语义搜索),本地 Community 版通过 vector search index 也可用。

来源与致谢 Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets