Lesson 4: Embedding Data in Documents / Learn

Press O for more options
3:03

Video Transcript (English)

Welcome! In this video, you’ll learn about how to model your data by using embedding. Embedding is often used when you have one-to-many or many-to-many relationships in the data that’s being stored.

MongoDB recommends embedding documents in order to simplify queries and improve overall query performance.

Let’s begin with an overview of embedded documents, also known as nested documents. These are documents that contain a document inside of another document. Embedded documents can range from fairly straightforward—such as “firstName lastName” embedded in a “name” field—to complex, with many embedded fields.

Embedded documents can be used for any type of relationship model. To illustrate this, consider this document with embedded subdocuments for name and address.

In the name field, the customer has only one name, Sarah Davis. This is embedded as firstName, lastName.

In the Address field, the customer has three addresses, so it’s a one-to-many relationship.

Nested documents like the one shown here make it easy to retrieve complete address information for a customer.

Embedding data like this is useful because it meets the principle of Data that is accessed together should be stored together.

Embedded documents enable us to store all kinds of related information in a single document. This can both simplify and reduce the number of queries required by your application.

Embedding avoids application joins, which minimizes queries, and provides better performance for read operations. Furthermore, embedded data models allow developers to update related data in a single write operation.

However, you should be aware of some issues that can arise when using embedded data models.

First, embedding data into a single document can, over time, make your document larger and larger as you add data.

Storing large documents in your database can lead to excessive memory and add latency for reads. This is because large documents must be read into memory in full. The result of this is often slow application performance for your end user.

Second, when embedding, you might accidentally structure your document in a way that data is added continuously, without limit. This creates what we refer to as an unbounded document.

Unbounded documents run the risk of exceeding the maximum BSON document size of 16 megabytes.

Both large documents and unbounded documents are schema anti-patterns, which you should avoid.You can read more about schema anti-patterns in the official MongoDB documentation, which you can find on our website.

Well done! Here are some key takeaways from this video:

Embedded documents capture relationships between data by storing related data in a single document.

MongoDB recommends embedding documents in order to simplify queries and improve overall query performance.

Embedding documents is ideal for one-to-many and many-to-many relationships among data.

Embedding also has some well-known pitfalls, such as large and unbounded documents, depending on query frequency.