Performing joins in MongoDB with $lookup

Eventually, it seems, looking up data in multiple MongoDB collections at the same time becomes necessary. Okay, necessary, might be strongly phrased. “Very helpful” may be better. Until version 3.2 of MongoDB doing joins wasn’t possible, but with that version came the $lookup aggregation stage operator. The introduction of $lookup allows for left outer joins to be performed on collections in the same database which are not sharded.

For our data for this post let’s utilize the concept of recipes. I’ll lay out some basic traditional SQL database tables and walk through the SQL syntax for a join.

Recipe table layout

SQL Syntax

Let’s have a quick look at what a left outer join looks like in SQL with a couple of different syntaxes in SQL.

Standard SQL
SELECT recipe_type.decription, recipes.title
FROM recipe_type
LEFT OUTER JOIN recipes
ON recipe_type.id = recipes.recipe_type_id
Oracle
SELECT recipe_type.description, recipes.title
FROM recipe_type, recipes
WHERE recipe_type.id = recipes.recipe_type_id(+)

The results of a left outer join for these two tables will contain all of the rows from our “left” table (recipe_type).

$lookup for Joins

Concept of a Left Outer Join
Concept of a Left Outer Join

Our join from $lookup then should be the same from a conceptual standpoint as our SQL cousin. Assuming we have a collection of recipe types and one of recipes our $lookup stage would look like the following:

{
  $lookup:
    {
      from: "recipes",
      localField: "id",
      foreignField: "recipe_type_id",
      as: "recipe_categories"
    }
}

Our $lookup stage then is doing an equality match between the two documents based on the id and recipe_type_id fields. It will add a new array field to the document in our aggregation pipeline document and passes it to the next stage of the pipeline.

Now, given our knowledge of schema design and document models in MongoDB we may not have a need for this exact join as these two collections of data might be embedded in one or the other collection. However, depending on your data access patterns and application needs, the collections may be utilizing references.

Wrap Up

I hope this provided a useful, and quick, introduction to the $lookup aggregation stage and how it is possible to do joins in MongoDB. While you may not find yourself using it all of the time, it is a great tool to have at the ready when it is needed.

There are several MongoDB specific terms in this post. I created a MongoDB Dictionary skill for the Amazon Echo line of products. Check it out and you can say “Alexa, ask MongoDB what is a document?” and get a helpful response.


Follow me on Twitter @kenwalger to get the latest updates on my postings.

Facebooktwitterredditlinkedinmail