Instagram
youtube
Facebook
Twitter

MongoDB Data Modeling

Data Modelling in MongoDB

  • Data modeling is the process of creating a clean data model of how you will store data in a database.
  • The key challenge in data modeling is balancing the needs of the application, the performance characteristics of the database engine, and the data retrieval patterns.
  • When designing data models, always consider the application usage of the data (i.e. queries, updates, and processing of the data) as well as the inherent structure of the data itself.

Data Model Design

Embedded Data Models

  • Embedded data models allow applications to store related pieces of information in the same database record.
  • In general, use embedded data models when: One-to-one relationships and one-to-many relationships.
  • Embedding provides better performance for read operations, as well as the ability to request and retrieve related data in a single database operation.
  • Embedded data models make it possible to update related data in a single atomic write operation.

Example:

{
_id:<ObjectId1>,
username: "123xyz",
contact:{
          phone: "12345667",
          email: "xyz@example.com:
        },
access:{
         level:5,
         group:"dev"
       }
}

Normalized Data Models

  • Normalized data models describe relationships using references between documents.

  • Normalized data models are used to represent the complex many-to-many relationship.

  • The normalized data model is used for large hierarchical data sets.