title: Mongodb $addFields, return field value from function?
body: <p>I am storing in my mongodb collection the video id of a video served in the cloud. When my front end makes an API call for that video, I want to return the formatted URL based on that video id. I have a function that does this, but I can't get it to work with <code>$addFields</code> in and <code>aggregate</code> pipeline:</p>
  
  <p>My documents look like this:</p>
  
  <pre><code>{
      "date" : ISODate("2018-03-30T00:00:00.000+0000"), 
      "title" : "Tips and Tricks", 
      "video_id" : "13966740", 
  }
  </code></pre>
  
  <p>...and I want my API call to return them with an extra field, <code>"image_url"</code>:</p>
  
  <pre><code>{
      "date" : ISODate("2018-03-30T00:00:00.000+0000"), 
      "title" : "Tips and Tricks", 
      "video_id" : "13966740", 
      "image_url" : "https://myhostingservice.com/13966740/preview_image", 
  }
  </code></pre>
  
  <p>This is what I'm trying in models/video.js:</p>
  
  <pre><code>const hostingservice = require('myhostingservicehelperfunctions');
  
  module.exports.getVideo = function (callback) {
      videoCollection.aggregate(
          [
              { $match: { } },
              { $addFields: {
                      image_url: hostingservice.createImageURL("$video_id")
                  }
              },
              { $sort: {
                  'date' : -1 }
              }
          ],
          callback);
  };
  </code></pre>
  
  <p>The helper function simply takes the string param and  returns a string. </p>
  
  <p><code>myhostingservicehelperfunctions.js</code>:</p>
  
  <pre><code>module.exports.createImageURL = function ImageURL(video_id){
      return 'https://myhostingservice.com/' + video_id + '/preview_image';
  };
  </code></pre>
  
  <p>My front end receives all the correct data, but the value of <code>image_url</code> is "<code>https://myhostingservice.com/$video_id/preview_image</code>", which makes me think my function ran, but it was passed the actual string <code>"$video_id"</code>, not the value of the key, <code>video_id</code>. Look closely, the URL contains <code>"$video_id"</code>, not <code>"13966740"</code> from the example above.</p>
  
  <p>What am I doing wrong here?</p>