title: Pass the value of a field name to an external function in a project stage
body: <p>I'm referring to <a href="https://stackoverflow.com/questions/67003225/trying-to-pass-the-value-of-a-field-to-an-external-function-inside-the-addfield">this post</a> but I can still not figure out how to solve this problem.</p>
  <p>In a <code>$project</code> stage, I'm trying to pass the value using a field name, but what is passed further is field name as a string (e.g., '$fieldname') instead of the reference value (e.g., 'a').</p>
  <p>I have understood the problem with client vs server side and read about <code>$function</code>, <code>mapReduce()</code> and <code>$where</code> alternatives. Since my mongodb version is &lt; 4.4 (4.2.13), I cannot use <code>$function</code> and I'm not sure I understand what the synthax should be with <code>$where or mapReduce()</code> when the called function is not defined in the body.</p>
  <p>Would someone be able to provide me with a concrete example how to resolve this issue ? I tried a basic example like:</p>
  <p>External function (another script):</p>
  <pre><code>function external_func(field_value) { // field_value is a string
      return field_value;
  }
  </code></pre>
  <p>In my aggregation pipeline:</p>
  <pre><code>...
  $project: {
      'new_field': {
                      $where: external_func(***) // -&gt; I would like to get the value of a field name which is a string in my collection
                  }
  }
  </code></pre>
  <p>***I tried using <code>external_func(this.field_name)</code>, <code>external_func('$field_name')</code> (among others) but the only response I get is <code>&quot;&quot;Unrecognized expression '$where'&quot;</code>.</p>
  <p>It looks like I may be too basic, but I have no idea. Thanks!</p>