Skip to content

Commit 527d3d6

Browse files
schmallisokay-kim
authored andcommitted
DOCS-11142: prefer $expr over $where
1 parent be2da06 commit 527d3d6

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

source/reference/operator/query/where.txt

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ $where
77
.. contents:: On this page
88
:local:
99
:backlinks: none
10-
:depth: 1
10+
:depth: 2
1111
:class: singlecol
1212

13+
Definition
14+
----------
15+
1316
.. query:: $where
1417

1518
Use the :query:`$where` operator to pass either a string
@@ -20,6 +23,14 @@ $where
2023
collection. Reference the document in the JavaScript expression or
2124
function using either ``this`` or ``obj`` .
2225

26+
.. important::
27+
.. versionchanged:: 3.6
28+
The :query:`$expr` operator allows the
29+
use of :ref:`aggregation expressions <aggregation-framework>`
30+
within the query language. :query:`$expr` is faster than
31+
:query:`$where` because it does not execute JavaScript and should
32+
be preferred where possible.
33+
2334
Behavior
2435
--------
2536

@@ -66,34 +77,43 @@ following performance advantages:
6677
- The non\-:query:`$where` query statements may use an
6778
:term:`index`.
6879

69-
Examples
70-
~~~~~~~~
71-
72-
Consider the following examples:
73-
74-
.. code-block:: javascript
75-
76-
db.myCollection.find( { $where: "this.credits == this.debits" } );
77-
db.myCollection.find( { $where: "obj.credits == obj.debits" } );
78-
79-
db.myCollection.find( { $where: function() { return (this.credits == this.debits) } } );
80-
db.myCollection.find( { $where: function() { return obj.credits == obj.debits; } } );
81-
82-
Additionally, if the query consists only of the :query:`$where`
83-
operator, you can pass in just the JavaScript expression or
84-
JavaScript functions, as in the following examples:
85-
86-
.. code-block:: javascript
87-
88-
db.myCollection.find( "this.credits == this.debits || this.credits > this.debits" );
89-
90-
db.myCollection.find( function() { return (this.credits == this.debits || this.credits > this.debits ) } );
80+
Example
81+
-------
9182

92-
You can include both the standard MongoDB operators and the
93-
:query:`$where` operator in your query, as in the following
94-
examples:
83+
Consider the following documents in the ``users`` collection:
9584

9685
.. code-block:: javascript
9786

98-
db.myCollection.find( { active: true, $where: "this.credits - this.debits < 0" } );
99-
db.myCollection.find( { active: true, $where: function() { return obj.credits - obj.debits < 0; } } );
87+
{
88+
_id: 12378,
89+
name: "Steve",
90+
username: "steveisawesome",
91+
first_login: "2017-01-01"
92+
}
93+
{
94+
_id: 2,
95+
name: "Anya",
96+
username: "anya",
97+
first_login: "2001-02-02"
98+
}
99+
100+
The following example uses :query:`$where` and the ``hex_md5()``
101+
JavaScript function to compare the value of the ``name`` field to an
102+
MD5 hash and returns any matching document.
103+
104+
.. code-block:: sh
105+
106+
db.foo.find( { $where: function() {
107+
return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994")
108+
} } );
109+
110+
The operation returns the following result:
111+
112+
.. code-block:: sh
113+
114+
{
115+
"_id" : 2,
116+
"name" : "Anya",
117+
"username" : "anya",
118+
"first_login" : "2001-02-02"
119+
}

0 commit comments

Comments
 (0)