$where¶
On this page
Definition¶
-
$where¶ Use the
$whereoperator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. The$whereprovides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. Reference the document in the JavaScript expression or function using eitherthisorobj.
Note
Starting in MongoDB 4.4, $where no longer supports the
deprecated BSON type JavaScript code with scope (BSON type 15). The $where operator only
supports BSON type String (BSON type 2) or BSON type JavaScript (BSON
type 13). The use of BSON type JavaScript
with scope for $where has been deprecated since MongoDB
4.2.1.
Aggregation Alternatives Preferred
Starting in MongoDB 3.6, the $expr operator allows the use
of aggregation expressions within the
query language. And, starting in MongoDB 4.4, the
$function and $accumulator allows users to
define custom aggregation expressions in JavaScript if the provided
pipeline operators cannot
fulfill your application’s needs.
Given the available aggregation operators:
- The use of
$exprwith aggregation operators that do not use JavaScript (i.e. non-$functionand non-$accumulatoroperators) is faster than$wherebecause it does not execute JavaScript and should be preferred if possible. - However, if you must create custom expressions,
$functionis preferred over$where.
Behavior¶
Available JavaScript Properties and Functions¶
map-reduce operations and $where
operator expressions cannot access certain global functions or
properties, such as db, that are available in the
mongo shell.
The following JavaScript functions and properties are available to
map-reduce operations and $where
operator expressions:
| Available Properties | Available Functions | |
|---|---|---|
argsMaxKeyMinKey |
assert()BinData()DBPointer()DBRef()doassert()emit()gc()HexData()hex_md5()isNumber()isObject()ISODate()isString() |
Map()MD5()NumberInt()NumberLong()ObjectId()print()printjson()printjsononeline()sleep()Timestamp()tojson()tojsononeline()tojsonObject()UUID()version() |
elemMatch¶
Only apply the $where query operator to top-level
documents. The $where query operator will not work inside a
nested document, for instance, in an $elemMatch query.
Considerations¶
- Do not use global variables.
$whereevaluates JavaScript and cannot take advantage of indexes. Therefore, query performance improves when you express your query using the standard MongoDB operators (e.g.,$gt,$in).- In general, you should use
$whereonly when you cannot express your query using another operator. If you must use$where, try to include at least one other standard query operator to filter the result set. Using$wherealone requires a collection scan.
Using normal non-$where query statements provides the
following performance advantages:
JavaScript Enablement¶
To use $where (or $function,
$accumulator, or mapReduce), you must have
server-side scripting enabled (default).
However, if you do not use these operations, disable server-side scripting:
For a
mongodinstance, seesecurity.javascriptEnabledconfiguration option or--noscriptingcommand-line option.For a
mongosinstance, seesecurity.javascriptEnabledconfiguration option or the--noscriptingcommand-line option starting in MongoDB 4.4.In earlier versions, MongoDB does not allow JavaScript execution onmongosinstances.
Example¶
Consider the following documents in the players collection:
The following example uses $where and the hex_md5()
JavaScript function to compare the value of the name field to an
MD5 hash and returns any matching document.
The operation returns the following result:
As an alternative, the previous example can be rewritten using
$expr and $function. Starting in MongoDB 4.4,
you can define custom aggregation expression in JavaScript with the
aggregation operator $function. To
access $function and other aggregation operators in
db.collection.find(), use with $expr:
If you must create custom expressions, $function is
preferred over $where.