$function
The $function operator lets you define and execute custom logic using JavaScript-like syntax within a pipeline.
📌 Syntax
{
"$function": {
"body": <functionCodeString>,
"args": [ <arg1>, <arg2>, ... ],
"lang": "js"
}
}
body: JavaScript function code as a stringargs: Array of arguments passed to the functionlang: Must be"js"(Fluxion-safe context)
✅ Base Example – Sum Two Fields
📥 Input Document
{ "a": 2, "b": 3 }
📌 Expression
{
"$function": {
"body": "function(a, b) { return a + b; }",
"args": ["$a", "$b"],
"lang": "js"
}
}
📤 Output
5
🧱 Ecommerce Example – Calculate Dynamic Discount
📌 Pipeline
[
{
"$project": {
"product": "$name",
"discountedPrice": {
"$function": {
"body": "function(p, d) {
return Math.round(p - (p * d));
}",
"args": ["$price", "$discount"],
"lang": "js"
}
}
}
}
]
📥 Input Document
{
"name": "Speaker",
"price": 200,
"discount": 0.15
}
📤 Output
{
"product": "Speaker",
"discountedPrice": 170
}
🔧 Common Use Cases
- Advanced conditional logic
- Complex business formulas
- Processing values beyond built-in operators
🔗 Related Operators
$cond,$switch,$reduce,$map
🧠Notes
- Fluxion executes
$functionsecurely and deterministically. - Avoid side effects; treat as pure functions.