Lecture 07.06 ModelingDataInMongo - 12
Lecture 07.06 ModelingDataInMongo - 12
in Mongo
JSON, BSON, Data Types,
References, Embedded
Documents
1
Key Data Structures in Mongo
MongoDB RDBMS Illustration
Collection Table {
{ {
item: 'almonds’,
item: 'almonds’,
price: item:
12, 'almonds’,
price:
quantity:
12,
price:
2 12, Collection
quantity: 2 2
quantity:
}
} }
Document Row
{
item: 'almonds’,
price: 12, Document
quantity: 2
}
Field Column
{
item: 'almonds’,
price: 12,
quantity: 2 Field
}
2 / 12
Documents and Collections
MongoDB groups data through collections.
• A collection is simply a grouping of documents that have the same or a similar purpose.
• A collection acts similarly to a table in a traditional SQL database. However, it has a major difference:
In MongoDB, a collection is not enforced by a strict schema. Instead, documents in a collection can
have a slightly different structure from one another, as needed. This reduces the need to break items
in a document into several different tables, as is often done in SQL implementations.
5 / 12
MongoDB Data Types
• The BSON data format provides several different data types used when
storing the JavaScript objects to binary form. These types match the
JavaScript type as closely as possible.
• It is important to understand these types because you can
actually query MongoDB to find objects that have a specific
property with a value of a certain type. For example, you can
look for documents in a database whose timestamp value is a
String object or query for ones whose timestamp is a Date object.
Embedding
Referencing
7 / 12
‘References’ in Mongo
8 / 12
‘Embedded’ Documents in
Mongo
9 / 12
Document References vs
Embedded Documents
Using document references is similar to using a
‘normalized’ data model and is used:
when embedding would result in duplication of data
but would not provide sufficient read performance
advantages to outweigh the implications of the
duplication.
to represent more complex many-to-many relationships.
to model large hierarchical data sets.
Using embedded documents is similar to using a
‘denormalized’ data model and is used:
you have “contains” relationships between entities.
you have one-to-many relationships between entities
10 / 12
RDBMS vs MongoDB
• Data is moved from rigid Tables to flexible and dynamic BSON documents that use data types, sub-
documents and arrays
• MongoDB document is stored as a single object, requiring only a single read from memory or disk
• As documents are self-contained, distributing the database across multiple nodes (a process called
sharding) becomes simpler and makes it possible to achieve massive horizontal scalability on
commodity hardware
• MongoDB documents tend to have all data for a given record in a single document, whereas in a relational
database information for a given record is usually spread across many tables.
• The complete document can be accessed with a single call to the database, rather than having to
JOIN multiple tables to respond to a query
• With the MongoDB document model, data is more localized, which significantly reduces the need to JOIN
separate tables.
• Structuring of data (via data models and schema) can be iterative and agile in MongoDB
• Data that belongs to a parent-child relationship in two RDBMS tables would commonly be collapsed
(embedded) into a single document in MongoDB
• The RDBMS optimizes data storage efficiency while MongoDB’s document model is optimized for how the
application accesses data (as developer time and speed to market are now more expensive than storage)
• MongoDB query model is implemented as methods or functions within the API of a specific
programming language, as opposed to a completely separate language like SQL.
• Application integration is simpler: MongoDB interface is implemented as methods (or functions) within
the API of a specific programming language, as opposed to a completely separate text-based
language like SQL. The affinity between MongoDB’s JSON document model and the data structures
used in object-oriented programming, makes integration with applications simple
• MongoDB provides document validation within the database. Users can enforce checks on document
structure, data types, data ranges and the presence of mandatory fields.
11 / 12
Document Model Advantages
Provide dramatically higher performance and scalability
across commodity hardware
A single read to the database can retrieve the entire
document containing all related data.
Don’t need to give up JOINs entirely
For additional analytics flexibility, MongoDB preserves left-
outer JOIN semantics with the $lookup operator, enabling
users to get the best of both relational and non-relational
data modeling.
MongoDB BSON documents are closely aligned to the
structure of objects in the programming language.
This makes it simpler and faster for developers to model
how data in the application will map to data stored in the
database.
12 / 12