diff --git a/source/includes/uncorrelated-subquery.rst b/source/includes/uncorrelated-subquery.rst new file mode 100644 index 00000000000..fd70caec15c --- /dev/null +++ b/source/includes/uncorrelated-subquery.rst @@ -0,0 +1,6 @@ +Starting in MongoDB 5.0, for an uncorrelated subquery in a +:pipeline:`$lookup` pipeline stage containing a :pipeline:`$sample` +stage, the :expression:`$sampleRate` operator, or the +:expression:`$rand` operator, the subquery is always run again if +repeated. Previously, depending on the subquery output size, either the +subquery output was cached or the subquery was run again. \ No newline at end of file diff --git a/source/reference/operator/aggregation/lookup.txt b/source/reference/operator/aggregation/lookup.txt index 68c72046ab0..d9e575aed6f 100644 --- a/source/reference/operator/aggregation/lookup.txt +++ b/source/reference/operator/aggregation/lookup.txt @@ -136,6 +136,10 @@ In MongoDB, a correlated subquery is a :ref:`pipeline document fields from a joined collection. An uncorrelated subquery does not reference joined fields. +.. note:: + + .. include:: /includes/uncorrelated-subquery.rst + MongoDB correlated subqueries are comparable to SQL correlated subqueries, where the inner query references outer query values. An SQL uncorrelated subquery does not reference outer query values. @@ -780,11 +784,13 @@ the ``warehouses`` collection: Perform an Uncorrelated Subquery with ``$lookup`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. versionchanged:: 3.6 +An aggregation pipeline :pipeline:`$lookup` stage can execute a pipeline +on the joined collection, which allows uncorrelated subqueries. An +uncorrelated subquery does not reference the joined document fields. + +.. note:: - MongoDB 3.6 adds support for executing a pipeline on the joined - collection, which allows uncorrelated subqueries. An uncorrelated - subquery does not reference the joined document fields. + .. include:: /includes/uncorrelated-subquery.rst Create a collection ``absences`` with these documents: diff --git a/source/release-notes/5.0.txt b/source/release-notes/5.0.txt index 7f4e18fbe50..a826bbcb70f 100644 --- a/source/release-notes/5.0.txt +++ b/source/release-notes/5.0.txt @@ -200,6 +200,13 @@ stage supports :ref:`concise correlated subqueries ` that improve joins between collections. +``$lookup`` Stage: Uncorrelated Subqueries +`````````````````````````````````````````` + +.. include:: /includes/uncorrelated-subquery.rst + +See :ref:`lookup-uncorrelated-subquery`. + .. _5.0-rel-notes-auditing: Auditing