$where
在此页面上
定义
$where
重要
服务器端 JavaScript 已弃用
Starting in MongoDB 8.0, server-side JavaScript functions (
$accumulator
,$function
,$where
) are deprecated. MongoDB logs a warning when you run these functions.Use the
$where
operator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. The$where
provides 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 eitherthis
orobj
.
兼容性
可以使用 $where
查找托管在以下环境中的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
$where
操作符采用以下形式:
{ $where: <string|JavaScript Code> }
注意
注意
Aggregation Alternatives Preferred
The $expr
operator allows the use of
aggregation expressions within
the query language. 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
$expr
with aggregation operators that do not use JavaScript (i.e. non-$function
and non-$accumulator
operators) is faster than$where
because it does not execute JavaScript and should be preferred if possible.However, if you must create custom expressions,
$function
is preferred over$where
。
行为
Available JavaScript Properties and Functions
map-reduce operations
和 $where
operator expressions 不能 access certain global functions or
properties, such as db
, that are available in
mongosh
。
The following JavaScript functions and properties are available to
map-reduce operations
and $where
operator expressions:
Available Properties | Available Functions | |
---|---|---|
args MaxKey MinKey | 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.
$where
evaluates 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
$where
only 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$where
alone 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:
对于
mongod
实例,请参阅security.javascriptEnabled
配置选项或--noscripting
命令行选项。对于
mongos
实例,请参阅security.javascriptEnabled
配置选项或--noscripting
命令行选项。
另请参阅 ➤使用安全配置选项运行 MongoDB。
不支持的数组与字符串函数
MongoDB 6.0 upgrades the internal JavaScript engine used for
server-side JavaScript,
$accumulator
, $function
, and $where
expressions and from MozJS-60 to MozJS-91. Several deprecated,
non-standard array and string functions that existed in MozJS-60 are
removed in MozJS-91.
有关已删除数组和字符串函数的完整列表,请参阅 6.0 兼容性说明。
例子
Consider the following documents in the players
collection:
db.players.insertMany([ { _id: 12378, name: "Steve", username: "steveisawesome", first_login: "2017-01-01" }, { _id: 2, name: "Anya", username: "anya", first_login: "2001-02-02" } ])
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.
db.players.find( { $where: function() { return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994") } } );
操作返回以下结果:
{ "_id" : 2, "name" : "Anya", "username" : "anya", "first_login" : "2001-02-02" }
As an alternative, the previous example can be rewritten using
$expr
and $function
. 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
:
db.players.find( {$expr: { $function: { body: function(name) { return hex_md5(name) == "9b53e667f30cd329dca1ec9e6a83e994"; }, args: [ "$name" ], lang: "js" } } } )
If you must create custom expressions, $function
is
preferred over $where
。