MongoDB Aggregation: $lookup for JOINs

As of MongoDB 3.2, a new feature has been added that introduces a left outer join for the first time.

$lookup is part of the MongoDB aggregation pipeline. It's a separate stage and the syntax is really simple:

db.orders.aggregate([
    {
      $lookup:
        {
          from: "inventory",
          localField: "item",
          foreignField: "sku",
          as: "inventory_docs"
        }
   }
])

The above command will fetch every document from the orders collection with the inventory_docs field in each one of them, populated with the matched documents from the inventory collection.

One will first notice that there is no way to specify matching conditions for the records to be joining the document. My hypothesis is that you're using an SQL data store in case you have many and complex relations between your entities. But in case that you are using a MongoDB data store while having that much relations, you will be disappointed.

Mongo's $lookup stage isn't coming to a step closer to traditional SQL join. It seems like a nice tool to have in your aggregation toolbox, but it won't cover you full if you need to do relational queries.

Comments

Navigate

Go canonical import paths & Github forks
27 February 2016
Automated GUI Testing Solutions
8 January 2016
I'm Kostas Bariotis, a web developer, a proud wanderer and a passionate doer. My mission is to write clean and efficient code, to solve problems on the Web and to learn something more. Read more about me or get in touch.