DbSchema Database Designer

Embedded Documents and Arrays | MongoDB Tutorial 2025



Embedded Documents and Arrays in MongoDb

  1. Introduction to MongoDB
  2. Installation & Database Creation
  3. CRUD Operations
  4. Embedded Documents and Arrays (You are here).

In MongoDB, data can be structured in a flexible way. One of the main features of MongoDB is the ability to store complex data structures in a single document. This is where Embedded Documents and Arrays come into play. Let’s dive into what these are, why they are beneficial, and when to use them, all while connecting to the Flights database we’ve been working with in the previous lessons.

What are Embedded Documents and Arrays?

Embedded Documents

An Embedded Document is a document nested inside another document. It allows you to store related data within a single document, making it easy to retrieve, update, or delete without requiring multiple queries or joins.

For example, in the Flights database, a Passenger document might have an embedded document representing their Address, like this:

db.passengers.insertMany([
{
"name": "Jennifer",
"age": 30,
"address": {
"street": "123 Elm St",
"city": "Springfield",
"zip": "12345"
}
}
])

Arrays

An Array is a collection of elements that can be of any type—strings, numbers, or even embedded documents. Arrays are especially useful when you want to store multiple values or related records for a single entity.

In our Flights database example, a passenger might carry multiple baggage items. Instead of creating separate documents for each bag, we can store them as an array of embedded documents within the passenger document.

Example: Array of Baggage Items

db.passengers.insertMany([
{
name: "Alice",
baggage: [
{ item: "Backpack", weight: 8 },
{ item: "Trolley", weight: 18 }
]
},
{
name: "Bob",
baggage: [
{ item: "Duffel Bag", weight: 10 }
]
}
])

Visualizing Embedded Documents and Arrays in DbSchema

Once you’ve inserted documents with embedded fields and arrays, you can see the structure directly in DbSchema’s collection diagram.

DbSchema displays:

  • Embedded documents as nested fields under the main collection.
  • Arrays (of values or documents) are shown with a [] symbol to indicate they contain multiple elements.

This makes it easy to explore the structure without writing any queries.

Example: Passenger with Embedded address and baggage[] Array

DbSchema Screenshot of Passengers Collection

In this example, the baggage[] field is an array of embedded documents, and address (if used) would appear as a nested structure inside the passengers collection.

Why are Embedded Documents and Arrays Useful?

1. Efficient Data Access

Embedded documents and arrays allow for fast, atomic operations since all related data is stored together. Instead of performing multiple queries and joins like in traditional relational databases, MongoDB can retrieve everything in a single query.

For example, when retrieving data for a Passenger, the Address and Baggage can be accessed within the same document, minimizing database lookups and improving performance.

2. Schema Flexibility

MongoDB’s flexible schema allows for varying document structures within the same collection. Some passengers might have baggage information, while others might not. You can easily handle this variation with embedded arrays.

{
"_id": ObjectId("..."),
"name": "Alice",
"baggage": []
}

3. Reduces Need for Joins

MongoDB is optimized for denormalized data. With embedded documents and arrays, there’s no need for complex joins as you’d have in SQL-based systems. You can store related information directly within the same document.

When to Use Embedded Documents and Arrays?

If the data is tightly related and accessed together, embedding it makes sense. For example, a Passenger’s Baggage can be embedded within the Passenger document, as they are always retrieved together.

2. Small, Static Data

If the related data is not expected to change often, it’s a good idea to embed it. For instance, the Address of a Passenger can be embedded since addresses typically don’t change frequently.

3. Avoiding the Need for Joins

If the data would normally require a join in a relational database, it may be a good candidate for embedding in MongoDB. This is particularly useful in cases where performance is critical, as MongoDB can retrieve all relevant data in one operation.

When Not to Use Embedded Documents:

Large Data:

If the embedded document or array is expected to grow rapidly or contain large data, it might be better to store it separately to avoid hitting MongoDB’s document size limit (16MB).

Frequently Updated Data:

If you frequently need to update certain elements of the embedded data, it may make sense to store them in separate documents for easier updates.

Connecting It to Our Flights Database

In our Flights database, we’ve seen that we can store documents for passengers and their related data. Here’s how embedded documents and arrays fit into our example:

Embedded Documents:

Each Passenger can have an Address or Passport Information embedded as a sub-document. These are closely related to the passenger and are likely to be queried together.

{
"_id": ObjectId("..."),
"name": "Mark",
"passport": {
"passport_number": "X123456",
"issue_country": "USA"
}
}

Arrays:

A Passenger might have multiple Bookings or Baggage items. We can store these as an array inside the Passenger document:

{
"_id": ObjectId("..."),
"name": "Alice",
"bookings": [
{ "flight_id": "AF123", "seat": "12A" },
{ "flight_id": "AF124", "seat": "14B" }
]
}

This allows us to retrieve all information about a passenger, including their flight bookings and baggage, in one go.

Conclusion

Embedded Documents and Arrays provide powerful ways to model complex data within MongoDB. They allow for more efficient queries, reduced need for joins, and flexible data storage. By leveraging these features, you can store data more naturally, keeping related information together.

In the case of our Flights database, using embedded documents and arrays helps us model passengers, bookings, baggage, and other related data in a straightforward and efficient manner. This approach ensures that all related data can be retrieved or updated in one operation, improving performance and making the data model easier to work with.

Next Lesson:

Learn how to enforce Data Validation Rules and ensure your MongoDB collections follow the defined structure.

Next Lesson

Visual Design & Schema Diagram

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.