7
7
.. contents:: On this page
8
8
:local:
9
9
:backlinks: none
10
- :depth: 1
10
+ :depth: 2
11
11
:class: singlecol
12
12
13
+ Definition
14
+ ----------
15
+
13
16
.. query:: $where
14
17
15
18
Use the :query:`$where` operator to pass either a string
@@ -20,6 +23,14 @@ $where
20
23
collection. Reference the document in the JavaScript expression or
21
24
function using either ``this`` or ``obj`` .
22
25
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
+
23
34
Behavior
24
35
--------
25
36
@@ -66,34 +77,43 @@ following performance advantages:
66
77
- The non\-:query:`$where` query statements may use an
67
78
:term:`index`.
68
79
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
+ -------
91
82
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:
95
84
96
85
.. code-block:: javascript
97
86
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