On this page本页内容
$function
¶New in version 4.4.版本4.4中的新功能。
Defines a custom aggregation function or expression in JavaScript.在JavaScript中定义自定义聚合函数或表达式。
You can use the 可以使用$function
operator to define custom functions to implement behavior not supported by the MongoDB Query Language. $function
运算符定义自定义函数,以实现MongoDB查询语言不支持的行为。See also 另见$accumulator
.$accumulator
。
Important
Executing JavaScript inside an aggregation expression may decrease performance. 在聚合表达式中执行JavaScript可能会降低性能。Only use the 仅当提供的管道运算符无法满足应用程序的需要时,才使用$function
operator if the provided pipeline operators cannot fulfill your application’s needs.$function
运算符。
The $function
operator has the following syntax:$function
运算符语法如下所示:
body | String |
|
args | Array |
|
lang | String |
|
You cannot use 不能将$function
as part of schema validation query expression.$function
用作架构验证查询表达式的一部分。
To use 要使用$function
, you must have server-side scripting enabled (default).$function
,必须启用服务器端脚本(默认)。
If you do not use 如果不使用$function
(or $accumulator
, $where
, or mapReduce
), disable server-side scripting:$function
(或$accumulator
、$where
或mapReduce
),请禁用服务器端脚本:
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。
$where
$where
之外的其他选择¶The query operator 查询运算符$where
can also be used to specify JavaScript expression. $where
也可用于指定JavaScript表达式。However:
$expr
operator allows the use of aggregation expressions within the query language.$expr
运算符允许在查询语言中使用聚合表达式。$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
和非$accumulator
运算符)一起使用比$where
更快,因为它不执行JavaScript,如果可能的话,应该优先使用。$function
is preferred over $where
.$function
优先于$where
。Create a sample collection named 使用以下文档创建名为players
with the following documents:players
的示例集合:
The following aggregation operation uses 以下聚合操作使用$addFields
to add new fields to each document:$addFields
向每个文档添加新字段:
isFound
whose value is determined by the custom $function
expression that checks whether the MD5 hash of the name is equal to a specified hash.isFound
的值由检查名称的MD5哈希是否等于指定哈希的自定义$function
表达式确定。message
whose value is determined by the custom $function
expression that format a string message using a template.$function
表达式确定的message
,该表达式使用模板格式化字符串消息。The operation returns the following documents:该操作将返回以下文档:
$where
$where
的选项¶Aggregation Alternatives Preferred over 聚合替代方案优先于$where
$where
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
和非$accumulator
运算符)一起使用比$where
更快,因为它不执行JavaScript,如果可能的话,应该优先使用。$function
is preferred over $where
.$function
优先于$where
。As an alternative to a query that uses the 作为使用$where
operator, you can use $expr
and $function
. $where
运算符的查询的替代方法,可以使用$expr
和$function
。For example, consider the following 例如,考虑下面的$where
example.$where
示例。
The db.collection.find()
operation returns the following document:db.collection.find()
操作返回以下文档:
The example can be expressed using 该示例可以使用$expr
and $function
:$expr
和$function
表示: