MongoDB is basically an open-source database tailored in consideration to agility and scalability. As opposed to storing data in table columns and rows as done in relational databases, MongoDB rather uses JSON similar documents that have dynamic schemas. Data modeling in MongoDB is quite similar to what happens in relational databases with slight differences. Essentially, data models are used to symbolize objects that an application will deal with together with the object relationships.
Data models in MongoDB
MongoDB represents data relationships between documents in two ways. First is similar to what is done in relational databases, where the primary key of one table can be used as a foreign key within a different table. This nature of the relationship in MongoDB is known as a reference. A good instance is a designation table that has an employee ID in the designation table (EmployeeID). This works similarly for the EmployeeID within a structured MongoDB employee document with respect to the employee ID in the Designation document.
The second method to model data in MongoDB document structure is through the embedding of documents within other documents commonly known as data embedding. Documents in MongoDB are like relational database records since they both have attributes for specific objects. However, it is impossible to embed records within records, hence, objects are just stored within specific records in their original formats. It is also possible to establish parent as well as child record relationships using several tables. Data embedding actually represents JSON data and within the data modeling, once the data is stored, it automatically becomes a JSON document and becomes a JSON object.
Advantages and disadvantages
The ability to embed documents within other documents positively impacts the storage of documents specifically with regard to economizing space. Embedding documents directly to other documents also helps when doing queries as you need a few atomic queries to match data. If this was done using relationship referencing, you must match against various documents, which lowers performance. In regard to large data sets, it is wise to minimize normalization and shift to less atomic queries for purposes of performance. But then regardless of this, relationship references are the best model when performance is not a consideration. Furthermore, through the embedding of documents within other documents at several layers, it allows for the representation of sophisticated objects through storing them as JSON strings in MongoDB.
Data referencing between documents is popular as it normalizes databases into smaller modular models, however, performance issues are fundamental as opposed to normalization and it is recommendable to embed data for performance. Normalized data in data referencing is a bit easier to work with as one entity update can help in a large dataset. Table to table relationships within MongoDB data referencing is complex as one must first ascertain if the foreign key relations have dependent data prior to deleting them.
The extensive document atomic operations within MongoDB data models allows for modelling of various distinct applications. Even in more demanding applications with dynamic data, it is possible to model them in MongoDB. The fundamental relationship between applications that rely majorly on code and those that rely on documents results to much simpler data models. The biggest advantage of both MongoDB data models is that it can best model one to many relationships in documents.