- Document model: MongoDB stores data as BSON documents, a binary representation of JSON-like structures. Each document contains field-value pairs where values can be strings, numbers, arrays, or nested documents. This model eliminates the need for joins and allows flexible schemas.
- Dynamic schema: Unlike relational tables with fixed columns, MongoDB collections can contain documents with different fields. This enables rapid iteration during development and supports varying data shapes within the same collection.
- Primary key: Every document requires a unique
_idfield. If omitted, MongoDB automatically generates an ObjectId value. The_idfield is immutable and indexed by default, providing efficient lookups. - Collections: Documents are grouped into collections, analogous to tables in relational databases. Collections are schema-less at creation, but developers often enforce consistency through application logic or MongoDB schema validation.
- Embedded documents: Related data can be embedded directly inside a parent document. For example, an order document may contain an array of line items. This reduces the need for expensive joins and improves read performance.
- References: For data that is accessed independently or has many-to-many relationships, MongoDB supports references using the
DBRefconvention or manual_idlinking. This is similar to foreign keys but resolved at the application layer. - BSON data types: BSON extends JSON with additional types such as Date, ObjectId, Binary, and Decimal128. These types enable efficient storage and querying of dates, large numbers, and binary data like images.
- Size limits: Each BSON document is limited to 16 MB. For larger data, use GridFS, which splits files into chunks stored across multiple documents. The 16 MB limit encourages designers to keep documents focused and small.
- Comparison with RDBMS: The document model provides better read performance for hierarchical data, supports agile schema changes, and scales horizontally. However, it sacrifices cross-document integrity and complex multi-table joins.
- Indexes: MongoDB supports multiple index types such as single field, compound, multikey, text, and geospatial indexes. Indexes dramatically speed up query execution by limiting the number of documents scanned. However, each index consumes storage and adds write overhead, so you must carefully plan index strategies based on your application's read and write patterns.
- Replication: MongoDB uses replica sets for high availability and data redundancy. A replica set consists of a primary node that handles all writes and multiple secondary nodes that replicate data asynchronously. If the primary fails, an election promotes a secondary to primary, ensuring minimal downtime and automatic failover. This architecture provides fault tolerance and disaster recovery without manual intervention.
- Sharding: For horizontal scaling, MongoDB distributes data across multiple servers using sharding. Data is partitioned by a shard key, and each shard holds a subset of the data. The mongos router directs queries to the appropriate shard, enabling linear scaling of throughput and storage for large datasets. Sharding is transparent to applications and requires careful selection of shard keys to avoid hotspots and uneven distribution.
db.users.insertOne({
name: 'Alice',
email: 'alice@example.com',
address: { street: '123 Maple St', city: 'Springfield' },
tags: ['premium', 'new']
})This command inserts a document with an embedded address object and an array of tags. The _id is automatically generated. The document structure can be extended later without altering existing documents.
- MongoDB uses a document model with BSON encoding, supporting dynamic schemas and embedded documents.
- Each document has a mandatory
_idfield, and collections group related documents. - BSON provides rich data types beyond JSON, and documents are limited to 16 MB.
- The document model offers flexibility and performance for hierarchical data compared to relational databases.