[DOCS-10894] $lookup docs should mention the $lookup+$unwind optimization Created: 13/Oct/17  Updated: 30/Oct/23  Resolved: 02/Nov/17

Status: Closed
Project: Documentation
Component/s: Server
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Task Priority: Major - P3
Reporter: Nathan Phillip Brink (binki) Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to DOCS-10791 Comment on: "manual/reference/operato... Closed
Participants:
Days since reply: 6 years, 14 weeks, 6 days ago

 Description   

When reading the docs for `$lookup`, I see that `as` says that the result shall be put in a document as an array. This immediately makes me think of the 16M BSON document size limitation and search the `$lookup` page for some note/discussion about the BSON document size limitation. I couldn’t figure out how `$lookup` would ever be useful if it always output the results into an array because for many large datasets it would be impossible to stay under the BSON document size limit.

Then I found https://stackoverflow.com/a/45726501/429091 which in the second paragraph states:

> The counter for this is to process with an `$unwind` which immediately follows the $lookup pipeline stage. This actually alters the behavior of `$lookup` in such that instead of producing an array in the parent, the results are instead a "copy" of each parent for every document matched.

Suddenly things made sense.

I think that this is a natural topic to be discussed/mentioned in the `$lookup` documentation. In fact, I think the `$lookup` documentation should *recommend* adding an `$unwind` stage immediately after to reduce the chance of hitting the document limit.



 Comments   
Comment by Nicholas Larew [ 02/Nov/17 ]

Thank you for submitting a Documentation ticket!

The $lookup + $unwind coalescence optimization is already documented in the Aggregation Pipeline Optimizations page.
There is also a note at the end of the "Definition" section of $lookup that links to the optimizations page.

Generated at Thu Feb 08 08:01:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.