$lookup (Join Collections)

Last updated: 2026-10-09

$lookup (Join Collections)

$lookup performs a left outer join between the current collection and another collection, attaching matching documents as an array to each result. Equivalent to SQL LEFT JOIN, it lets pipelines combine data across collections without multiple queries. Note it only supports equality matches — no range or conditional joins.

CategoryCore Stages
MongoDB Version3.2+
Example Languagemongosh (Shell)

📝 Syntax

{ $lookup: { from: <collection>, localField: <local-field>, foreignField: <foreign-field>, as: <output-array> } }

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $lookup: join orders with customers
db.orders.aggregate([
  { $lookup: {
      from: "customers",
      localField: "cust_id",
      foreignField: "_id",
      as: "customer"
  } }
])

Output:

{ "_id": 1, "cust_id": "A123", "customer": [{ "_id": "A123", "name": "Alice" }] }

📌 Notes

📚 Related Tutorials

❓ FAQ

QCan $lookup replace database-level JOINs?
AIt achieves similar results, but $lookup merges data at query time: equality matches and simple comparisons inside the pipeline can use indexes, yet its join optimization is less comprehensive than SQL databases, so evaluate performance on very large datasets.
QHow can I optimize $lookup performance?
ACreate an index on the foreign key field of the joined collection. Also prefer $match early in the pipeline to shrink the main collection before the join.
QDoes $lookup support multi-key joins?
AYes. Since 3.6, the pipeline form of $lookup with $expr enables multi-field joins and subqueries beyond simple equality on one key.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏