Sling Academy
Home/MongoDB/MongoDB: Using lookUp() to merge reference relations

MongoDB: Using lookUp() to merge reference relations

Last updated: February 01, 2024

The $lookup Stage

The $lookup stage in the aggregation pipeline allows us to perform a left outer join to a collection in the same database to filter in documents from the joined collection for processing. The basic $lookup syntax is as follows:

db.collection.aggregate([
  {
    $lookup:
    {
      from: "collection to join", 
      localField: "field from the input documents",
      foreignField: "field from the documents of the 'from' collection",
      as: "output array field"
    }
  }
]);

Let’s write a query to join books with authors:

db.books.aggregate([
  {
    $lookup:
    {
      from: "authors", 
      localField: "author_id",
      foreignField: "_id",
      as: "authorDetails"
    }
  }
]).pretty();

This would produce documents where each book now includes an array called ‘authorDetails’ containing the joined author document(s).

Handling Multiple Matches

What happens if our join condition matches multiple documents? In such cases, the $lookup stage will append all matching documents to the output array. For our example, since each book has a single author, we should get exactly one match per book. However, if you’re referencing a collection with the possibility of multiple matches and need to handle this, MongoDB will naturally handle this by appending each matching document to the specified array.

Combining $lookup with Other Stages

The real power of the aggregation pipeline comes from combining various stages. Let’s use $lookup with $match to filter results:

db.books.aggregate([
  {
    $match: { title: 'Pride and Prejudice' }
  },
  {
    $lookup:
    {
      from: 'authors',
      localField: 'author_id',
      foreignField: '_id',
      as: 'authorDetails'
    }
  }
]).pretty();

We first filter books where the title is ‘Pride and Prejudice’ and then perform our $lookup.

Optimizing $lookup Performance

Although $lookup stages are powerful, they may cause performance hits, especially with large datasets or many lookups. Optimizations include:

  • Making sure that the foreign field (the field from the joined collection) is indexed.
  • Limiting the amount of data processed at each stage by using $match, $project, etc., before the $lookup stage.

Conclusion

In this tutorial, we’ve seen how to use the $lookup aggregation stage in MongoDB to join documents from separate collections, effectively ‘merging’ reference relations. By mastering $lookup and combining it with other pipeline stages, you can perform complex data retrievals and transformations that can harness the full power of MongoDB’s flexible document model.

Next Article: MongoDB: Using Projection in Arrays – Tutorial & Examples

Previous Article: One-to-Many Relations in MongoDB: The Ultimate Guide (with examples)

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)