CHAPTER 4 Data Models Data in MongoDB has a flexible schema . Collections do not enforce document structure. This flexibility gives you data-modeling choices to match your application and its performance requirements. Data Modeling Concepts (page 145) The core documentation detailing the decisions you must make when determin- ing a data model, and discussing considerations that should be taken into account. Data Model Examples and Patterns (page 151) Examples of possible data models that you can use to structure your MongoDB documents. Data Model Reference (page 168) Reference material for data modeling for developers of MongoDB applications. Data Modeling Introduction (page 143) An introduction to data modeling in MongoDB. 4.1 Data Modeling Introduction Data in MongoDB has a flexible schema . Unlike SQL databases, where you must determine and declare a table’s schema before inserting data, MongoDB’s collections do not enforce document structure. This flexibility facilitates the mapping of documents to an entity or an object. Each document can match the data fields of the represented entity, even if the data has substantial variation. In practice, however, the documents in a collection share a similar structure. 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. The key decision in designing data models for MongoDB applications revolves around the structure of documents and how the application represents relationships between data. There are two tools that allow applications to represent these relationships: references and embedded documents . References store the relationships between data by including links or references from one document to another. Appli- cations can resolve these references (page 171) to access the related data. Broadly, these are normalized data models. 4.1.1 Document Structure References See Normalized Data Models (page 147) for the strengths and weaknesses of using references. 143