对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$function (流处理)

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

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

or

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

阵列

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 lang: "js".

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.

给本页内容打分

在此页面上