$function (aggregation)
在此页面上
定义
$function
重要
服务器端 JavaScript 已弃用
Starting in MongoDB 8.0, server-side JavaScript functions (
$accumulator
,$function
,$where
) are deprecated. MongoDB logs a warning when you run these functions.Defines a custom aggregation function or expression in JavaScript.
You can use the
$function
operator to define custom functions to implement behavior not supported by the MongoDB Query Language. See also$accumulator
.重要
Executing JavaScript inside an aggregation expression may decrease performance. Only use the
$function
operator if the provided pipeline operators cannot fulfill your application's needs.
语法
$function
操作符的语法如下:
{ $function: { body: <code>, args: <array expression>, lang: "js" } }
字段 | 类型 | 说明 |
---|---|---|
字符串或代码 | The function definition. You can specify the function definition as either BSON type Code or String. See also lang.
or
| |
阵列 | Arguments passed to the function body.
If the body function does not take an
argument, you can specify an empty array The array elements can be any BSON type, including Code. See
Example 2: Alternative to | |
字符串 | The language used in the body. You
must specify |
Considerations
Schema Validation Restriction
You cannot use $function
as part of a schema
validation query predicate.
Javascript Enablement
To use $function
, you must have server-side scripting
enabled (default).
如果您不使用 $function
(或 $accumulator
、$where
或 mapReduce
),请禁用服务器端脚本:
对于
mongod
实例,请参阅security.javascriptEnabled
配置选项或--noscripting
命令行选项。对于
mongos
实例,请参阅security.javascriptEnabled
配置选项或--noscripting
命令行选项。In earlier versions, MongoDB does not allow JavaScript execution onmongos
instances.
另请参阅 ➤使用安全配置选项运行 MongoDB。
Alternative to $where
The query operator $where
can also be used to specify
JavaScript expression. However:
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.
给定可用的聚合操作符:
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
.
不支持的数组与字符串函数
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 兼容性说明。
示例
Example 1: Usage Example
创建一个包含以下文档的样本集合 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:
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.message
whose value is determined by the custom$function
expression that format a string message using a template.
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" } } } } ] )
该操作将返回以下文档:
{ "_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." }
Example 2: Alternative to $where
注意
Aggregation Alternatives Preferred over $where
The $expr
operator allows the use of
aggregation expressions within the
query language. And the $function
and $accumulator
allows users to define custom aggregation expressions in JavaScript if the
provided pipeline operators cannot fulfill your application's needs.
给定可用的聚合操作符:
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
.
As an alternative to a query that uses the $where
operator,
you can use $expr
and $function
. For example,
consider the following $where
example.
db.players.find( { $where: function() { return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad") } } );
The db.collection.find()
operation returns the following document:
{ "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ] }
The example can be expressed using $expr
and $function
:
db.players.find( {$expr: { $function: { body: function(name) { return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"; }, args: [ "$name" ], lang: "js" } } } )