Video Transcript (English)
Welcome! In this lesson, you’ll learn how traditional relational database components translate to MongoDB database components.
Let’s start by mapping traditional relational database components to MongoDB
While MongoDB and traditional relational databases share many of the same key terms, there are also a lot of differences.
Let’s start with the term database. Both MongoDB and SQL use the term database.
In a relational database, a table is a series of rows, where each row is a specific entry.
Meanwhile, in MongoDB, the equivalent term is collection, which is a set of documents.
In a relational database, data is stored in a row. In MongoDB, data is stored in a document.
Let’s go through a quick example. Here we have a row of data for an employee, Mary Jones.
And here’s the same employee data in a MongoDB document.
In both cases, the same data is stored; it’s merely in a different structural format.
A table in a relational database has a parent-child relationship to other tables by linking attributes in a one-to-one, one-to-many, or many-to-many fashion.
MongoDB also expresses relationships between collections. In addition, in MongoDB we can embed a document within a document as a nested document or array. This results in a simplified data model because embedding documents results in fewer collections.
A column of a table translates to a field of a document. However, each document can have a different list of fields, which allows for documents with different shapes in a collection.
Index is another key term shared between MongoDB and relational databases. Like relational databases, indexes in MongoDB ensure good performance. However, they are built in different ways.
In a relational database, indexes are built on columns in a table. In MongoDB, an index is built on a set of fields in a collection. This is like building an index on a set of tables, as a collection is a composition of tables.
One key difference between relational databases and MongoDB is the use of joins. In relational databases, joins are used to get data from two tables.
MongoDB uses a $lookup stage to perform the same link between collections. Some relationships are expressed by embedding the child table, so there’s no need to join anything. This makes queries simpler.
Finally, two other terms that are shared between relational databases and MongoDB are views and transactions. Relational views are also called views in MongoDB.
Transactions in relational databases are also known as transactions in MongoDB.
Now that you understand some basic MongoDB terminology, we’ll show some examples of SQL rows and tables, and then translate them into an equivalent MongoDB document.
When using SQL, we have a table named “users”. There are columns created for each piece of contact information that we need to store: first name, last name, phone number, and age.
To ensure unique identity, we’ll include an ID column. Here, you can see a row created for Mira Chan with these pieces of information.
Storing this same information in MongoDB is easy. There is a document with field/value pairs for each piece of Mira’s information. Instead of a table, this document will be stored in a collection named “Users”.
Now let’s add more information to our user table in SQL.
If we collected multiple addresses for our user Mira Chan, we would probably create another table called “addresses”.
This table would be joined to the “users” table via a user_id.
But in MongoDB, we insert the addresses directly into the same document.
To do this, we use the array data type, which can contain values of any type, including objects. This is what we call embedding documents.
This is much easier for developers to keep track of, because Mira’s data is stored together, as opposed to in different tables via a lookup or join.
Great! Now you can map SQL components to MongoDB components. This simple example illustrates how easy it is to work with your data in the document format, and how intuitive MongoDB is when storing your data.