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
。
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
运算符语法如下:
{ $function: { body: <code>, args: <array expression>, lang: "js" } }
body | String or Code |
|
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
命令行选项。For a 对于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
的示例集合:
db.players.insertMany([ { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] }, { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] }, { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] } ])
The following aggregation operation uses 以下聚合操作使用$addFields
to add new fields to each document:$addFields
向每个文档添加新字段:
isFound
$function
expression that checks whether the MD5 hash of the name is equal to a specified hash.$function
表达式确定,该表达式检查名称的MD5哈希是否等于指定的哈希。message
$function
expression that format a string message using a template.$function
表达式确定。db.players.aggregate( [ { $addFields: { isFound: { $function: { body: function(name) { return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad" }, args: [ "$name" ], lang: "js" } }, message: { $function: { body: function(name, scores) { let total = Array.sum(scores); return `Hello ${name}. Your total score is ${total}.` }, args: [ "$name", "$scores"], lang: "js" } } } } ] )
The operation returns the following documents:该操作返回以下文档:
{ "_id" : 1, "name" : "Miss Cheevous", "scores" : [ 10, 5, 10 ], "isFound" : false, "message" : "Hello Miss Cheevous. Your total score is 25." } { "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ], "isFound" : true, "message" : "Hello Miss Ann Thrope. Your total score is 30." } { "_id" : 3, "name" : "Mrs. Eppie Delta ", "scores" : [ 9, 8, 8 ], "isFound" : false, "message" : "Hello Mrs. Eppie Delta . Your total score is 25." }
$where
$where
$where
The $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
示例。
db.players.find( { $where: function() { return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad") } } );
The db.collection.find()
operation returns the following document:db.collection.find()
操作返回以下文档:
{ "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ] }
The example can be expressed using 示例可以使用$expr
and $function
:$expr
和$function
表示:
db.players.find( {$expr: { $function: { body: function(name) { return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"; }, args: [ "$name" ], lang: "js" } } } )