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表示: