Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/
Atlas
/ /

$function (Stream Processing)

Note

Currently, this functionality is only available in Atlas Stream Processing Instances deployed to AWS cloud.

The $function expression specifies a custom JavaScript function that you can define to run JavaScript code on each document in the stream.

The $function operator has the following syntax:

{
$function: {
body: <code>,
args: <array expression>,
lang: "js"
}
}
Field
Type
Description

body

String or Code

The function definition. You can specify the function definition as either BSON type Code or String. See also lang.

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

or

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

Array

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 $where.

String

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.

Back

$createUUID

On this page