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.
Instalación lista para agent
Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.
npx -y tokrepo@latest install a2aa9815-35f3-11f1-9bc6-00163e2b0d79 --target codexEjecutar después de confirmar el plan con dry-run.
What it is
MongoDB is a source-available, cross-platform document database that stores data in flexible JSON-like documents (BSON format). Unlike relational databases that require rigid schemas, MongoDB documents can have varying structures, making it suitable for applications with evolving data models. It supports horizontal scaling through sharding, automatic replication for high availability, and rich query capabilities including aggregation pipelines.
Backend developers building web and mobile applications, data engineers working with semi-structured data, and teams needing flexible schemas use MongoDB as their primary data store. It is one of the most widely adopted NoSQL databases.
How it saves time or tokens
Relational databases require defining schemas upfront and running migrations for every structure change. MongoDB allows you to store documents with different fields in the same collection, eliminating migration overhead for evolving applications. The aggregation pipeline provides complex data transformations in the database layer, reducing application code. Native JSON-like storage means less serialization work when your application already works with JSON objects.
How to use
- Install MongoDB:
# macOS via Homebrew
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 and insert data:
// Using mongosh (MongoDB Shell)
use mydb
db.users.insertOne({
name: 'Alice',
email: 'alice@example.com',
tags: ['developer', 'admin'],
created: new Date()
})
- Query your data:
db.users.find({ tags: 'developer' })
db.users.aggregate([
{ $match: { tags: 'developer' } },
{ $group: { _id: null, count: { $sum: 1 } } }
])
Example
# Python with PyMongo
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client['myapp']
# Insert documents
db.products.insert_many([
{'name': 'Widget', 'price': 9.99, 'category': 'tools', 'stock': 150},
{'name': 'Gadget', 'price': 24.99, 'category': 'electronics', 'stock': 42},
])
# Query with filter and projection
results = db.products.find(
{'price': {'$lt': 20}},
{'name': 1, 'price': 1, '_id': 0}
)
# Aggregation pipeline
pipeline = [
{'$group': {'_id': '$category', 'avg_price': {'$avg': '$price'}}},
{'$sort': {'avg_price': -1}}
]
for doc in db.products.aggregate(pipeline):
print(doc)
Related on TokRepo
- Database Tools -- explore database tools and management solutions
- AI Tools for API Development -- discover API tools that integrate with document databases
Common pitfalls
- MongoDB does not enforce schemas by default. Use schema validation rules on collections to prevent malformed documents in production.
- Unbounded array growth in documents causes performance degradation. Avoid patterns where arrays grow indefinitely; use separate collections for one-to-many relationships with large cardinality.
- The default write concern is w:1 (acknowledged by primary only). For data durability, set w:'majority' to ensure writes are replicated before acknowledgment.
Preguntas frecuentes
MongoDB works well for applications with flexible or evolving schemas, document-centric data models, and horizontal scaling needs. PostgreSQL is better for relational data with complex joins, ACID transactions across multiple tables, and applications that benefit from rigid schema enforcement.
Yes. MongoDB supports multi-document ACID transactions since version 4.0. Transactions work across multiple documents, collections, and databases within a replica set or sharded cluster. However, single-document operations are already atomic, so many use cases do not need explicit transactions.
MongoDB uses sharding to distribute data across multiple servers. You choose a shard key that determines how documents are distributed. The mongos router directs queries to the appropriate shard. Auto-balancing moves chunks between shards to maintain even distribution.
The aggregation pipeline is a data processing framework that transforms documents through a sequence of stages like $match, $group, $sort, $project, and $lookup. It runs inside the database, avoiding the need to transfer large datasets to the application for processing.
MongoDB is source-available under the Server Side Public License (SSPL) since 2018. The SSPL requires that companies offering MongoDB as a service must open-source their entire service stack. MongoDB Community Edition is free to use, while MongoDB Atlas is the managed cloud offering.
Referencias (3)
- MongoDB GitHub— Cross-platform document database with flexible JSON-like storage
- MongoDB Documentation— Aggregation pipeline and sharding for horizontal scaling
- MongoDB Transactions— Multi-document ACID transactions since version 4.0
Relacionados en TokRepo
Discusión
Activos relacionados
PostgreSQL — The Most Advanced Open Source Relational Database
PostgreSQL is the most powerful open-source relational database system. It combines SQL compliance, extensibility, and reliability with advanced features like JSONB, full-text search, vector embeddings (pgvector), and PostGIS — making it the database of choice for modern applications.
Databasus — Self-Hosted Database Backup Tool with Web Dashboard
An open-source database backup tool supporting PostgreSQL, MySQL/MariaDB, and MongoDB. Provides scheduled backups, S3-compatible storage, encryption, and a clean web UI for managing backup jobs.
Adminer — Full-Featured Database Management in a Single PHP File
Adminer is a lightweight, single-file PHP database management tool that supports MySQL, PostgreSQL, SQLite, MS SQL, Oracle, and MongoDB. It provides a clean web UI for browsing tables, running queries, managing users, importing and exporting data, all from one deployable PHP file under 500 KB.
Bootstrap — The Most Popular CSS Framework for Responsive Web Design
Mobile-first front-end toolkit with prebuilt components, a responsive grid system, and powerful JavaScript plugins for building modern websites fast.