|
| 1 | +.. _streams-agg-pipeline-function: |
| 2 | +.. _atlas-sp-agg-function: |
| 3 | + |
| 4 | +============================================== |
| 5 | +``$function`` Stage (Stream Processing) |
| 6 | +============================================== |
| 7 | + |
| 8 | +.. default-domain:: mongodb |
| 9 | + |
| 10 | +.. meta:: |
| 11 | + :keywords: atlas stream processing, $function aggregation pipeline stage |
| 12 | + :description: Learn how to use the $function stage to aggregate data across |
| 13 | + overlapping or staggered timeframes. |
| 14 | + |
| 15 | +.. facet:: |
| 16 | + :name: genre |
| 17 | + :values: reference |
| 18 | + |
| 19 | +.. contents:: On this page |
| 20 | + :local: |
| 21 | + :backlinks: none |
| 22 | + :depth: 2 |
| 23 | + :class: singlecol |
| 24 | + |
| 25 | +.. _atlas-sp-agg-function-def: |
| 26 | + |
| 27 | +.. note:: |
| 28 | + |
| 29 | + **Currently, this functionality is only available in Atlas Stream Processing |
| 30 | + Instances deployed to AWS cloud.** |
| 31 | + |
| 32 | + |
| 33 | +Definition |
| 34 | +~~~~~~~~~~ |
| 35 | + |
| 36 | +The :expression:`$function` stage specifies a pipeline stage in which you can run |
| 37 | +custom JavaScript code locally within the pipeline. Currently, this functionality |
| 38 | +is only available in Atlas clusters deployed to AWS infrastructure. |
| 39 | + |
| 40 | +.. _atlas-sp-agg-function-syntax: |
| 41 | + |
| 42 | +Syntax |
| 43 | +------ |
| 44 | + |
| 45 | +The :expression:`$function` operator has the following syntax: |
| 46 | + |
| 47 | +.. code-block:: javascript |
| 48 | + |
| 49 | + { |
| 50 | + $function: { |
| 51 | + body: <code>, |
| 52 | + args: <array expression>, |
| 53 | + lang: "js" |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | +.. list-table:: |
| 58 | + :header-rows: 1 |
| 59 | + :widths: 20 20 80 |
| 60 | + |
| 61 | + * - Field |
| 62 | + - Type |
| 63 | + - Description |
| 64 | + |
| 65 | + |
| 66 | + * - :ref:`body <function-body>` |
| 67 | + |
| 68 | + - String or Code |
| 69 | + |
| 70 | + - .. _function-body: |
| 71 | + |
| 72 | + The function definition. You can specify the function |
| 73 | + definition as either BSON type Code or String. See also |
| 74 | + :ref:`lang <function-lang>`. |
| 75 | + |
| 76 | + ``function(arg1, arg2, ...) { ... }`` |
| 77 | + |
| 78 | + or |
| 79 | + |
| 80 | + ``"function(arg1, arg2, ...) { ... }"`` |
| 81 | + |
| 82 | + |
| 83 | + * - :ref:`args <function-args>` |
| 84 | + |
| 85 | + - Array |
| 86 | + |
| 87 | + - .. _function-args: |
| 88 | + |
| 89 | + Arguments passed to the function :ref:`body <function-body>`. |
| 90 | + If the :ref:`body <function-body>` function does not take an |
| 91 | + argument, you can specify an empty array ``[ ]``. |
| 92 | + |
| 93 | + The array elements can be any BSON type, including Code. See |
| 94 | + :ref:`function-example-where-alternative`. |
| 95 | + |
| 96 | + * - :ref:`lang <function-lang>` |
| 97 | + |
| 98 | + - String |
| 99 | + |
| 100 | + - .. _function-lang: |
| 101 | + |
| 102 | + The language used in the :ref:`body <function-body>`. You |
| 103 | + must specify ``lang: "js"``. |
| 104 | + |
| 105 | + |
| 106 | +.. _atlas-sp-agg-function-behavior: |
| 107 | + |
| 108 | +Behavior |
| 109 | +-------- |
| 110 | + |
| 111 | +The :expression:`$function` stage runs the specified function |
| 112 | +on each document in the stream. The function can take arguments |
| 113 | +from the document or from the :ref:`args <function-args>` array. |
| 114 | + |
| 115 | +The function can return any BSON type, including an array or a |
| 116 | +Code type. The returned value is used as the output of the |
| 117 | +:expression:`$function` stage. |
0 commit comments