On this page本页内容
$where
¶Use the 使用$where
operator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. $where
运算符将包含JavaScript表达式的字符串或完整JavaScript函数传递给查询系统。The $where
provides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. $where
提供了更大的灵活性,但要求数据库处理集合中每个文档的JavaScript表达式或函数。Reference the document in the JavaScript expression or function using either 使用this
or obj
.this
或obj
在JavaScript表达式或函数中引用文档。
Note
Starting in MongoDB 4.4, 从MongoDB 4.4开始,$where
no longer supports the deprecated BSON type JavaScript code with scope (BSON type 15). $where
不再支持不推荐使用的BSON类型JavaScript代码(BSON类型15)。The $where
operator only supports BSON type String (BSON type 2) or BSON type JavaScript (BSON type 13). $where
运算符仅支持BSON类型字符串(BSON类型2)或BSON类型JavaScript(BSON类型13)。The use of BSON type JavaScript with scope for 自MongoDB 4.2.1以来,使用作用域为$where
has been deprecated since MongoDB 4.2.1.$where
的BSON类型JavaScript已被弃用。
Aggregation Alternatives Preferred聚合替代品优先
Starting in MongoDB 3.6, the 从MongoDB 3.6开始,$expr
operator allows the use of aggregation expressions within the query language. $expr
运算符允许在查询语言中使用聚合表达式。And, starting in MongoDB 4.4, the 而且,从MongoDB 4.4开始,$function
and $accumulator
allows users to define custom aggregation expressions in JavaScript if the provided pipeline operators cannot fulfill your application’s needs.$function
和$accumulator
允许用户在提供的管道运算符无法满足应用程序的需要时,在JavaScript中定义自定义聚合表达式。
Given the available aggregation operators:考虑到可用的聚合运算符:
$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.$expr
与不使用JavaScript的聚合运算符(即非$function
和非$acculator
运算符)一起使用比$where
更快,因为它不执行JavaScript,如果可能的话,应该优先使用。$function
is preferred over $where
.$function
优先于$where
。map-reduce操作和map-reduce operations
and $where
operator expressions cannot access certain global functions or properties, such as db
, that are available in the mongo
shell.$where
运算符表达式无法访问mongo
shell中可用的某些全局函数或属性,例如db
。
The following JavaScript functions and properties are available to 以下JavaScript函数和属性可用于map-reduce operations
and $where
operator expressions:map-reduce
操作和$where
运算符表达式:
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. $where
查询运算符应用于顶级文档。The $where
query operator will not work inside a nested document, for instance, in an $elemMatch
query.$where
查询运算符在嵌套文档中不起作用,例如在$elemMatch
查询中。
$where
evaluates JavaScript and cannot take advantage of indexes. $where
评估JavaScript,无法利用索引。$gt
, $in
).$gt
、$in
)表达查询时,查询性能会提高。$where
only when you cannot express your query using another operator. $where
。$where
, try to include at least one other standard query operator to filter the result set. $where
,请尝试至少包含一个其他标准查询运算符来筛选结果集。$where
alone requires a collection scan.$where
需要集合扫描。Using normal non-使用普通的非$where
query statements provides the following performance advantages:$where
查询语句具有以下性能优势:
$where
components of query before $where
statements. $where
语句之前评估查询的非$where
组件。$where
statements match no documents, MongoDB will not perform any query evaluation using $where
.$where
句不匹配任何文档,MongoDB将不会使用$where
执行任何查询求值。$where
query statements may use an index.$where
查询语句可以使用索引。To use 要使用$where
(or $function
, $accumulator
, or mapReduce
), you must have server-side scripting enabled (default).$where
(或$function
、$accumulator
或mapReduce
),必须启用服务器端脚本(默认)。
However, if you do not use these operations, disable server-side scripting:但是,如果不使用这些操作,请禁用服务器端脚本:
mongod
instance, see security.javascriptEnabled
configuration option or --noscripting
command-line option.mongod
实例,请参阅security.javascriptEnabled
配置选项或--noscripting
命令行选项。mongos
instance, see security.javascriptEnabled
configuration option or the --noscripting
command-line option starting in MongoDB 4.4.mongos
实例,请参阅security.javascriptEnabled
配置选项或从MongoDB 4.4开始的--noscripting
命令行选项。
mongos
instances.mongos
实例上执行JavaScript。See also ➤ Run MongoDB with Secure Configuration Options.请参阅➤ 使用安全配置选项运行MongoDB。
Consider the following documents in the 考虑以下文件在players
collection:players
集合:
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.$where
和hex_md5()
JavaScript函数将name
字段的值与md5哈希值进行比较,并返回任何匹配的文档。
The operation returns the following result:操作返回以下结果:
As an alternative, the previous example can be rewritten using 作为替代方案,可以使用$expr
and $function
. $expr
和$function
重写前面的示例。Starting in MongoDB 4.4, you can define custom aggregation expression in JavaScript with the aggregation operator 从MongoDB 4.4开始,可以使用聚合运算符$function
. $function
在JavaScript中定义自定义聚合表达式。To access 访问$function
and other aggregation operators in db.collection.find()
, use with $expr
:db.collection.find()
中的$function
和其他聚合运算符,请与$expr
一起使用:
If you must create custom expressions, 如果必须创建自定义表达式,则首选$function
is preferred over $where
.$function
而不是$where
。