MongoDB is the most popular document-oriented NoSQL database. It stores JSON-like BSON documents, and its schema-less nature enables flexible data modeling. It supports high availability and horizontal scaling through replica sets and sharding.
Document-Oriented Data Model - MongoDB uses a collection/document structure instead of tables/rows. Documents are stored in BSON (Binary JSON) format, which is similar to JSON, and naturally represent nested objects and arrays. Data that would be spread across multiple tables in a relational DB can be embedded in a single document and queried without a JOIN.
Schema Flexibility - There is no need to define a schema for a collection in advance. Documents within the same collection can have different fields, which makes it well suited to rapidly changing requirements. Since MongoDB 3.6, optional schema validation with JSON Schema is also possible.
Replica Set - This is MongoDB's high-availability solution. It consists of one Primary and several Secondaries, and automatic failover occurs when the Primary fails. Read load can be distributed across Secondaries, and data replication ensures durability.
Sharding - This is a horizontal scaling method that distributes large volumes of data across multiple servers. Data is partitioned based on the Shard Key, and the mongos router forwards queries to the appropriate shards. It can handle petabyte-scale data.
Aggregation Pipeline - This is a powerful framework for data transformation and analysis. Complex data processing is possible by chaining stages such as $match, $group, $sort, and $lookup (JOIN). It performs better than Map-Reduce and is recommended for most analysis tasks.
"I'm wondering how to store product and review data. If there are an average of 10 or fewer reviews per product, it's better to embed them in the product document; if there are more, it's better to split them into a separate collection and join them with $lookup. You also need to consider the 16MB document size limit."
💬 When Responding to Performance Issues
"Looking at the explain() results, a COLLSCAN is occurring. You need to add a compound index on category and createdAt. Also, if you add a projection to find() and fetch only the necessary fields, you can reduce network costs as well."
💬 In a Scalability Design Meeting
"If the data grows to the TB scale, sharding is necessary. Choosing the Shard Key is important, and you should pick a field with high cardinality that is frequently used in queries, such as user_id. If you choose wrongly, hotspots will occur and you will have to reshard later."
⚠️ Cautions & Best Practices
❌
Avoid Indiscriminate Embedding
Document size is limited to 16MB. Split arrays that can grow indefinitely (comments, logs, etc.) into a separate collection.
❌
Beware of Queries Without Indexes
Querying large amounts of data without an index results in a COLLSCAN. Check the execution plan with explain() and create appropriate indexes.
❌
Avoid Overusing Transactions
MongoDB transactions have greater overhead than an RDBMS. Where possible, design around single-document operations, and use transactions only when truly necessary.
✅
MongoDB Best Practices
Proper data modeling (embedding vs. referencing), establishing an index strategy, high availability with replica sets, using Change Streams, and reduced management burden when using Atlas.