Share
Explore

The Princess, the Dragons, and the Wisdom of Margong

Once upon a time in the Kingdom of Data, Princess Hermia was tasked by the King to slay all the dragons terrorizing the villages.
Dutifully, she set about organizing her quest. She created two scrolls (collections):
Dragons Scroll: A list of all dragons, each with their name and the village they were said to haunt.
Villages Scroll: A list of all villages, each with its name and details about the people living there.

The Encounter with Margong

When Hermia met Margong, the first dragon, she was surprised. Margong was not a villain, but a wise, helpful dragon, beloved by the villagers. Hermia realized things were more complicated than the King had told her.
Margong said, ​"Princess, you have two scrolls, but to truly understand what’s happening, you must see how dragons and villages are connected. Let me show you the magic of $lookup and $unwind!"

Margong’s Lesson: $lookup

Margong explained, ​"Imagine you want to see, for each dragon, which village they are connected to. You use a magic spell called $lookup. This spell reads the Dragons Scroll, and for each dragon, it finds the matching village from the Villages Scroll, based on the village name."
In code, it looks like:
{
$lookup: {
from: "villages",
localField: "villageName",
foreignField: "name",
as: "villageInfo"
}
}

"Now, each dragon’s entry includes a new field, villageInfo, which is a list (array) of matching villages."

Margong’s Lesson: $unwind

Hermia noticed that villageInfo was an array, even though each dragon haunted only one village.
Margong smiled and said,
"To make it easier to work with, you can use another spell: $unwind. This will take the array of villages and lay them out flat, so each dragon is paired directly with their village."
In code, it looks like:
{ $unwind: "$villageInfo" }

"Now, for every dragon, you have a single, clear record showing the dragon and their village side by side. No more confusing lists within lists!"

The Princess’s Insight

With Margong’s wisdom, Hermia saw the truth:
Some dragons were helping the villagers.
Some villages were protected by dragons.
The King’s orders were based on misleading summaries, not the real relationships.
By using $lookup, Hermia could join information from two scrolls (collections), and with $unwind, she could flatten the results for easy understanding.

The Moral

$lookup is like asking, ​"For each dragon, show me the villages they are connected to."
$unwind is like saying, ​"Don’t give me a list of villages per dragon—just give me one dragon and one village per line, so I can see each connection clearly."
And so, Princess Hermia used these magical tools to bring peace and understanding to the kingdom—proving that with the right perspective, even dragons and villagers can live happily ever after.
In summary:
$lookup joins two collections, adding related information as arrays.
$unwind flattens those arrays, making relationships clear and easy to work with—just as Margong taught Hermia!

$lookup joins two collections, adding related information as arrays. The collections are joined based on one field which is common to the 2 collections.

The $lookup stage in MongoDB’s aggregation pipeline joins two collections by matching documents where a field in the first collection (the “local field”) equals a field in the second collection (the “foreign field”).
The result is that related information from the second collection is added as an array field in the output documents.

In summary:

$lookup joins two collections.
The join is based on a field that is common (or related) between the two collections.
The matching documents from the second collection are added as an array to each document from the first collection.

Example

image.png

A $lookup like this:
{
$lookup: {
from: "villages",
localField: "villageName",
foreignField: "name",
as: "villageInfo"
}
}
will join the two collections where dragons.villageName matches villages.name.
The collections are joined based on one field which is common (or related) to the two collections.
This is the core idea behind $lookup in MongoDB!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.