MongoDB
Overview
MongoDB is a document-oriented NoSQL database that stores flexible, JSON-like documents (BSON) in collections, well suited for evolving schemas and nested data. Read this when modeling document data, designing indexes, writing queries/aggregations, or choosing between embedding and referencing.
Official sources
Install / setup
Download MongoDB Community Server from mongodb.com/try/download/community for your platform (Ubuntu, Debian, RedHat/CentOS, SUSE, Amazon Linux, macOS, Windows). MongoDB Atlas is the managed cloud alternative; its quick setup uses Homebrew:
brew install mongodb-atlas
atlas setup
For self-hosted Community Server, follow the platform-specific install guide at https://www.mongodb.com/docs/manual/administration/install-community/.
Core concepts
- Documents and collections: documents (BSON) are grouped into collections; no fixed schema is required.
- Flexible schema: fields can vary between documents in the same collection.
- Indexes: single-field, compound, multikey (arrays), text, and others; needed for query performance.
- Aggregation pipeline: stages (
$match, $group, $lookup, etc.) transform and compute over documents.
- Embedding vs referencing: model related data inline (embed) or via references depending on access patterns.
- Replica sets and sharding: replica sets provide HA; sharding partitions data horizontally across shards.
Best practices
- Model schema around your application's query patterns; embed data that is read together, reference data that grows unboundedly.
- Create indexes that support your queries and sorts; confirm usage with
explain().
- Use the aggregation pipeline for server-side data transformation instead of pulling and processing in the app.
- Prefer a replica set (even of one for dev parity) so transactions and failover behave as in production.
- Use schema validation rules when you need to enforce document shape.
Common pitfalls
- Treating MongoDB like a relational DB with many
$lookup joins → reconsider the data model; embed where appropriate.
- Letting documents grow without bound (e.g., ever-growing arrays) → can exceed the 16 MB document limit; use references/bucketing.
- Forgetting indexes on filtered/sorted fields → full collection scans.
- Assuming multi-document atomicity without transactions → use transactions (requires a replica set) when you need them.
Examples
db.users.insertOne({
email: "a@example.com",
profile: { name: "Ada", roles: ["admin"] },
createdAt: new Date()
});
db.users.createIndex({ email: 1 }, { unique: true });
Further reading
Related skills
- ../dynamodb — managed key-value/document NoSQL alternative
- ../redis — in-memory store for caching and ephemeral data
1---2name: mongodb3description: Document database storing flexible JSON-like (BSON) documents; consult for schema design, indexing, aggregation, and NoSQL data modeling.4domain: stack5category: database6tags: [mongodb, nosql, document, bson, database]7official_sources:8 - https://www.mongodb.com/docs/9 - https://github.com/mongodb/mongo10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/mongodb13---1415# MongoDB1617## Overview18MongoDB is a document-oriented NoSQL database that stores flexible, JSON-like documents (BSON) in collections, well suited for evolving schemas and nested data. Read this when modeling document data, designing indexes, writing queries/aggregations, or choosing between embedding and referencing.1920## Official sources21- Docs: https://www.mongodb.com/docs/22- Repo (official; SSPL v1 license for releases after 2018-10-16): https://github.com/mongodb/mongo23- Download (Community Server): https://www.mongodb.com/try/download/community2425## Install / setup26Download MongoDB Community Server from mongodb.com/try/download/community for your platform (Ubuntu, Debian, RedHat/CentOS, SUSE, Amazon Linux, macOS, Windows). MongoDB Atlas is the managed cloud alternative; its quick setup uses Homebrew:2728```bash29brew install mongodb-atlas30atlas setup31```3233For self-hosted Community Server, follow the platform-specific install guide at https://www.mongodb.com/docs/manual/administration/install-community/.3435## Core concepts36- Documents and collections: documents (BSON) are grouped into collections; no fixed schema is required.37- Flexible schema: fields can vary between documents in the same collection.38- Indexes: single-field, compound, multikey (arrays), text, and others; needed for query performance.39- Aggregation pipeline: stages (`$match`, `$group`, `$lookup`, etc.) transform and compute over documents.40- Embedding vs referencing: model related data inline (embed) or via references depending on access patterns.41- Replica sets and sharding: replica sets provide HA; sharding partitions data horizontally across shards.4243## Best practices44- Model schema around your application's query patterns; embed data that is read together, reference data that grows unboundedly.45- Create indexes that support your queries and sorts; confirm usage with `explain()`.46- Use the aggregation pipeline for server-side data transformation instead of pulling and processing in the app.47- Prefer a replica set (even of one for dev parity) so transactions and failover behave as in production.48- Use schema validation rules when you need to enforce document shape.4950## Common pitfalls51- Treating MongoDB like a relational DB with many `$lookup` joins → reconsider the data model; embed where appropriate.52- Letting documents grow without bound (e.g., ever-growing arrays) → can exceed the 16 MB document limit; use references/bucketing.53- Forgetting indexes on filtered/sorted fields → full collection scans.54- Assuming multi-document atomicity without transactions → use transactions (requires a replica set) when you need them.5556## Examples57```javascript58db.users.insertOne({59 email: "a@example.com",60 profile: { name: "Ada", roles: ["admin"] },61 createdAt: new Date()62});6364db.users.createIndex({ email: 1 }, { unique: true });65```6667## Further reading68- Data modeling: https://www.mongodb.com/docs/manual/data-modeling/69- Aggregation: https://www.mongodb.com/docs/manual/aggregation/7071## Related skills72- ../dynamodb — managed key-value/document NoSQL alternative73- ../redis — in-memory store for caching and ephemeral data