定义
The $function expression specifies a custom JavaScript function that you can define to run JavaScript code on each document in the stream.
重要
$function 默认禁用。在使用此阶段之前,请联系 MongoDB 支持以为项目启用功能标志。
语法
The $function operator has the following syntax:
{ $function: { body: <code>, args: <array expression>, lang: "js" } }
字段 | 类型 | 说明 |
|---|---|---|
字符串或代码 | 函数定义。可以将函数定义指定为 BSON 类型 Code 或 String。另请参阅 lang。
or
| |
阵列 | Arguments passed to the function body. If the body function does not take an argument, you can specify an empty array 大量元素可以是任何BSON类型,包括 Code。请参阅示例 2:$where 的替代方案。 | |
字符串 | The language used in the body. You must specify |
行为
The $function expression runs the specified function on each document in the stream. The function can take arguments from the document or from the args array.
The function can return any BSON type, including an array or a Code type. The returned value is used as the output of the $function expression.