Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-10894

$lookup docs should mention the $lookup+$unwind optimization

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • None
    • Server
    • None

    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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            binki Nathan Phillip Brink (binki)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              6 years, 14 weeks, 6 days ago