Skip to content

Commit 984bf0b

Browse files
authored
DOCSP-34259 re-use $expr example on find() (#6057)
* DOCSP-34259 re-use $expr example on find() * internal review
1 parent 0908a24 commit 984bf0b

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Compare Two Fields from A Single Document
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Consider a ``monthlyBudget`` collection with the following documents:
5+
6+
.. code-block:: javascript
7+
8+
{ "_id" : 1, "category" : "food", "budget": 400, "spent": 450 }
9+
{ "_id" : 2, "category" : "drinks", "budget": 100, "spent": 150 }
10+
{ "_id" : 3, "category" : "clothes", "budget": 100, "spent": 50 }
11+
{ "_id" : 4, "category" : "misc", "budget": 500, "spent": 300 }
12+
{ "_id" : 5, "category" : "travel", "budget": 200, "spent": 650 }
13+
14+
The following operation uses :query:`$expr` to find documents
15+
where the ``spent`` amount exceeds the ``budget``:
16+
17+
.. code-block:: javascript
18+
19+
db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )
20+
21+
The operation returns the following results:
22+
23+
.. code-block:: javascript
24+
25+
{ "_id" : 1, "category" : "food", "budget" : 400, "spent" : 450 }
26+
{ "_id" : 2, "category" : "drinks", "budget" : 100, "spent" : 150 }
27+
{ "_id" : 5, "category" : "travel", "budget" : 200, "spent" : 650 }

source/reference/method/db.collection.find.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ field does not exists:
373373

374374
For a list of the query operators, see :ref:`query-selectors`.
375375

376+
.. include:: /includes/use-expr-in-find-query.rst
377+
376378
.. _query-subdocuments:
377379
.. _query-embedded-documents:
378380

source/reference/operator/query/expr.txt

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,7 @@ If the :pipeline:`$match` stage is part of a :pipeline:`$lookup` stage,
5959
Examples
6060
--------
6161

62-
Compare Two Fields from A Single Document
63-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64-
65-
Consider an ``monthlyBudget`` collection with the following documents:
66-
67-
.. code-block:: javascript
68-
69-
{ "_id" : 1, "category" : "food", "budget": 400, "spent": 450 }
70-
{ "_id" : 2, "category" : "drinks", "budget": 100, "spent": 150 }
71-
{ "_id" : 3, "category" : "clothes", "budget": 100, "spent": 50 }
72-
{ "_id" : 4, "category" : "misc", "budget": 500, "spent": 300 }
73-
{ "_id" : 5, "category" : "travel", "budget": 200, "spent": 650 }
74-
75-
The following operation uses :query:`$expr` to find documents
76-
where the ``spent`` amount exceeds the ``budget``:
77-
78-
.. code-block:: javascript
79-
80-
db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )
81-
82-
The operation returns the following results:
83-
84-
.. code-block:: javascript
85-
86-
{ "_id" : 1, "category" : "food", "budget" : 400, "spent" : 450 }
87-
{ "_id" : 2, "category" : "drinks", "budget" : 100, "spent" : 150 }
88-
{ "_id" : 5, "category" : "travel", "budget" : 200, "spent" : 650 }
89-
62+
.. include:: /includes/use-expr-in-find-query.rst
9063

9164
Using ``$expr`` With Conditional Statements
9265
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)