# 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. ## Install Save the content below to `.claude/skills/` or append to your `CLAUDE.md`: ## Quick Use ```bash # 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 ``` ```javascript // 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. - **Repo**: https://github.com/mongodb/mongo - **Stars**: 28K+ - **Language**: C++ - **License**: SSPL (Source Available) ## 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 ```bash # 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 can do 80% of what MongoDB can (plus ACID and a mature SQL ecosystem). MongoDB's advantages are horizontal scaling, flexible schema, and a popular ORM (Mongoose). **Q: What does SSPL mean?** A: Source Available, but restricts SaaS commercial use (if you turn MongoDB into a managed service, you must open-source the entire service stack). Personal use and embedding in applications are completely fine. **Q: Vector search?** A: MongoDB Atlas has built-in vector search starting from v7 (RAG, semantic search). The local Community edition can also use it via the vector search index. ## Sources - Docs: https://www.mongodb.com/docs - GitHub: https://github.com/mongodb/mongo - License: SSPL --- Source: https://tokrepo.com/en/workflows/mongodb-most-popular-document-database-a2aa9815 Author: Script Depot