Data Modeling
Data modeling refers to the organization of data within a database and the links between related entities. Data in MongoDB has a flexible schema model, which means:
Documents within a single collection are not required to have the same set of fields.
A field's data type can differ between documents within a collection.
Generally, documents in a collection share a similar structure. To ensure consistency in your data model, you can create schema validation rules.
Use Cases
The flexible data model lets you organize your data to match your application's needs. MongoDB is a document database, meaning you can embed related data in object and array fields.
A flexible schema is useful in the following scenarios:
Your company tracks which department each employee works in. You can embed department information inside of the
employee
collection to return relevant information in a single query.Your e-commerce application shows the five most recent reviews when displaying a product. You can store the recent reviews in the same collection as the product data, and store older reviews in a separate collection because the older reviews are not accessed as frequently.
Your clothing store needs to create a single-page application for a product catalog. Different products have different attributes, and therefore use different document fields. However, you can store all of the products in the same collection.
Schema Design: Differences between Relational and Document Databases
When you design a schema for a document database like MongoDB, there are a couple of important differences from relational databases to consider.
Relational Database Behavior | Document Database Behavior |
---|---|
You must determine a table's schema before you insert data. | Your schema can change over time as the needs of your application change. |
You often need to join data from several different tables to return the data needed by your application. | The flexible data model lets you store data to match the way your application returns data, and avoid joins. Avoiding joins across multiple collections improves performance and reduces your deployment's workload. |
Plan Your Schema
To ensure that your data model has a logical structure and achieves optimal performance, plan your schema prior to using your database at a production scale. To determine your data model, use the following Apply design patterns.