Skip to content

DOCS-14252 create match expressions for aggregation #5087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/includes/expr-operators-and-indexes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Starting in MongoDB 5.0, the :expression:`$eq`, :expression:`$lt`,
:expression:`$lte`, :expression:`$gt`, and :expression:`$gte` comparison
operators placed in an :query:`$expr` operator can use an index on the
``from`` collection referenced in a :pipeline:`$lookup` stage.
Limitations:

- :ref:`Multikey indexes <index-type-multikey>` are not used.

- Indexes are not used for comparisons where the operand is an array or
the operand type is undefined.

- Indexes are not used for comparisons with more than one :ref:`field
path <agg-quick-ref-field-paths>` operand.
13 changes: 5 additions & 8 deletions source/includes/fact-let-variable-access-note.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
.. note::

To reference variables in :ref:`pipeline <lookup-join-pipeline>`
stages, use the ``"$$<variable>"`` syntax.
stages, use the ``"$$<variable>"`` syntax.

The :ref:`let <lookup-join-let>` variables can be accessed by the
stages in the :ref:`pipeline <lookup-join-pipeline>`, including
additional :pipeline:`$lookup` stages nested in the ``pipeline``.

- A :pipeline:`$match` stage requires the use of an
:query:`$expr` operator to access the variables.
:query:`$expr` allows the use of aggregation expressions
inside of the :pipeline:`$match` syntax.
:query:`$expr` operator to access the variables. The :query:`$expr`
operator allows the use of aggregation expressions inside of the
:pipeline:`$match` syntax.

The :query:`$expr` operator only uses indexes on the joined
``from`` collection for equality matches. Non-equality match
queries, such as range queries, cannot use indexes on the joined
``from`` collection.
.. include:: /includes/expr-operators-and-indexes.rst

- Other (non-:pipeline:`$match`) stages in the :ref:`pipeline
<lookup-join-pipeline>` do not
Expand Down
12 changes: 6 additions & 6 deletions source/reference/operator/aggregation/lookup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Definition

.. pipeline:: $lookup

.. versionadded:: 3.2
.. versionchanged:: 5.0

Performs a left outer join to an unsharded collection in the *same*
database to filter in documents from the "joined" collection for
Expand Down Expand Up @@ -772,16 +772,16 @@ The operation corresponds to this pseudo-SQL statement:
AND instock >= orders.ordered
);

The :query:`$expr` operator only uses indexes on the ``from``
collection for equality matches. For example, if the index
``{ stock_item: 1, instock: 1 }`` exists on the ``warehouses``
collection:
.. include:: /includes/expr-operators-and-indexes.rst

For example, if the index ``{ stock_item: 1, instock: 1 }`` exists on
the ``warehouses`` collection:

- The equality match on the ``warehouses.stock_item`` field uses the
index.

- The range part of the query on the ``warehouses.instock`` field
does not use the indexed field in the compound index.
also uses the indexed field in the compound index.

.. seealso::

Expand Down
18 changes: 7 additions & 11 deletions source/reference/operator/query/expr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $expr
Definition
----------

.. versionadded:: 3.6
.. versionchanged:: 5.0

.. query:: $expr

Expand All @@ -33,18 +33,14 @@ Definition
Behavior
--------

- :query:`$expr` can build query expressions that compare fields
from the same document in a :pipeline:`$match` stage.
:query:`$expr` can build query expressions that compare fields from the
same document in a :pipeline:`$match` stage.

- If the :pipeline:`$match` stage is part of a :pipeline:`$lookup`
stage, :query:`$expr` can compare fields using ``let`` variables.
See :ref:`lookup-multiple-joins` for an example.
If the :pipeline:`$match` stage is part of a :pipeline:`$lookup` stage,
:query:`$expr` can compare fields using ``let`` variables. See
:ref:`lookup-multiple-joins` for an example.

- :query:`$expr` only uses indexes on the ``from`` collection for
equality matches in a :pipeline:`$match` stage.

- :query:`$expr` does not support
:ref:`multikey indexes <index-type-multikey>`.
.. include:: /includes/expr-operators-and-indexes.rst

Examples
--------
Expand Down
7 changes: 7 additions & 0 deletions source/release-notes/5.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Starting in MongoDB 5.0, you can specify multiple input expressions for
the :expression:`$ifNull` expression before returning a replacement
expression.

Comparison Operators and Indexes with ``$expr`` Operator
````````````````````````````````````````````````````````

Starting in MongoDB 5.0, the :expression:`$eq`, :expression:`$lt`,
:expression:`$lte`, :expression:`$gt`, and :expression:`$gte` operators
placed in an :query:`$expr` operator can use an index.

Aggregate ``let`` Option
````````````````````````

Expand Down