ScriptsApr 11, 2026·3 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.

TL;DR
MongoDB stores data in flexible JSON-like documents with horizontal scaling, replication, and sharding built in.
§01

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.

§02

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.

§03

How to use

  1. 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
  1. 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()
})
  1. Query your data:
db.users.find({ tags: 'developer' })
db.users.aggregate([
  { $match: { tags: 'developer' } },
  { $group: { _id: null, count: { $sum: 1 } } }
])
§04

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)
§05

Related on TokRepo

§06

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

When should I use MongoDB instead of PostgreSQL?+

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.

Does MongoDB support transactions?+

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.

How does MongoDB handle horizontal scaling?+

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.

What is the MongoDB aggregation pipeline?+

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.

Is MongoDB still open source?+

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)

Discussion

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

Related Assets