Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

$function(聚合)

在此页面上

  • 定义
  • 语法
  • 考虑因素
  • 举例
$function

在 JavaScript 中定义一个自定义聚合函数或表达式。

您可以使用 $function操作符定义自定义函数,以实施 MongoDB 查询语言不支持的行为。另请参阅$accumulator

重要

在聚合表达式中执行 JavaScript 可能会降低性能。仅当提供的$function 管道操作符 无法满足应用程序的需求时,才使用 操作符。

$function操作符的语法如下:

{
$function: {
body: <code>,
args: <array expression>,
lang: "js"
}
}
字段
类型
说明
body
字符串或代码

函数定义。您可以将函数定义指定为 BSON 类型 Code 或 String。另请参阅lang。

function(arg1, arg2, ...) { ... }

"function(arg1, arg2, ...) { ... }"

阵列

传递给函数的参数。如果函数不带参数,则可以指定一个空数组[ ]

数组元素可以是任何 BSON 类型,包括 Code。请参阅示例2 : $where的替代方案。

字符串

正文中使用的语言。您必须指定lang: "js"

不能使用$function作为模式验证查询表达式的一部分。

要使用$function ,必须启用服务器端脚本(默认)。

如果不使用$function (或$accumulator$wheremapReduce ),请禁用服务器端脚本:

另请参阅 ➤使用安全配置选项运行 MongoDB

查询操作符$where也可用于指定 JavaScript 表达式。然而:

  • $expr 运算符允许在查询语言中使用聚合表达式

  • 如果提供的管道操作符无法满足应用程序的需求,则$function$accumulator允许用户在 JavaScript 中定义自定义聚合表达式。

给定可用的聚合操作符:

MongoDB6 。0 升级用于 服务器端 JavaScript 、 、$accumulator $function和 表达式的内部$where JavaScript 引擎,并从 MozJS-60 升级到 MozJS-91 。 MozJS- 中存在的几个已弃用的非标准数组和字符串函数已在60 MozJS-91 中删除。

有关已删除数组和字符串函数的完整列表,请参阅 6.0 兼容性说明

创建一个包含以下文档的样本集合 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 ] }
])

下面的聚合操作使用 $addFields 为每份文档添加新字段:

  • isFound 其值由自定义$function表达式确定,该表达式检查名称的 MD 5哈希是否等于指定哈希。

  • message 其值由使用模板格式化字符串消息的自定义$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"
}
}
}
}
] )

该操作将返回以下文档:

{ "_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

$expr操作符允许在查询语言中使用聚合表达式。如果提供的管道操作符无法满足应用程序的需求,则$function$accumulator允许用户在 JavaScript 中定义自定义聚合表达式。

给定可用的聚合操作符:

作为使用$where操作符的查询的替代方案,您可以使用$expr$function 。例如,请考虑以下$where示例。

db.players.find( { $where: function() {
return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
} } );

db.collection.find() 操作会返回以下文档:

{ "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ] }

该示例可以使用$expr$function

db.players.find( {$expr: { $function: {
body: function(name) { return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"; },
args: [ "$name" ],
lang: "js"
} } } )
← $floor(聚合)