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.
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.
Frequently Asked Questions
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.
Citations (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
Related on TokRepo
Discussion
Related Assets
Moodle — Open-Source Learning Management System
The most widely used open-source learning platform, providing course management, assessments, and collaboration tools for educators and organizations worldwide.
Sylius — Headless E-Commerce Framework on Symfony
An open-source headless e-commerce platform built on Symfony and API Platform, designed for developers who need a customizable and API-first commerce solution.
Akaunting — Free Self-Hosted Accounting Software
A free, open-source online accounting application built on Laravel for small businesses and freelancers to manage invoices, expenses, and financial reports.