title: MongoDB View vs Function to abstract query and variable/parameter passed
body: <p>I hate to risk asking a duplicate question, but perhaps this is different from <a href="https://stackoverflow.com/questions/52786830/passing-variables-to-a-mongodb-view">Passing Variables to a MongoDB View</a> which didn't have any clear solution.</p>
  <p>Below is a query to find the country for IP Address 16778237. (Outside the scope of this query, there is a formula that turns an IPV4 address into a number.)</p>
  <p>I was wondering if we could abstract away this query out of NodeJS code, and make a view, so the view could be called from NodeJS. But the fields ipFrom and ipTo are indexed to get the query to run fast against millions of documents in the collection, so we can't return all the rows to NodeJS and filter there.</p>
  <p>In MSSQL maybe this would have to be a stored procedure, instead of a view.  Just trying to learn what is possible in MongoDB.  I know there are functions, which are written in JavaScript.  Is that where I need to look?</p>
  <pre><code>db['ip2Locations'].aggregate(
      {
          $match: 
          {
              $and: [
                  {
                      &quot;ipFrom&quot;: {
                          $lte: 16778237
                      }
                  },
                  {
                      &quot;ipTo&quot;: {
                          $gte: 16778237
                      }
                  },
                  {
                      &quot;active&quot;: true
                  }
              ],
                      $comment: &quot;where 16778237 between startIPRange and stopIPRange and the row is 'Active',sort by createdDateTime, limit to the top 1 row, and return the country&quot;
          }
      },
      {
          $sort: 
          {
              'createdDateTime': - 1
          }
      },
      {
          $project: 
          {
              'countryCode': 1
          }
      },
      {
          $limit: 1
      }
  )
  </code></pre>
  <p>Part 2 - after more research and experimenting, I found this is possible and runs with success, but then see trying to make a view below this query.</p>
  <pre><code>var ipaddr = 16778237
  db['ip2Locations'].aggregate(
      {
          $match: 
          {
              $and: [
                  {
                      &quot;ipFrom&quot;: {
                          $lte: ipaddr
                      }
                  },
                  {
                      &quot;ipTo&quot;: {
                          $gte: ipaddr
                      }
                  },
                  {
                      &quot;active&quot;: true
                  }
              ],
                      $comment: &quot;where 16778237 between startIPRange and stopIPRange and the row is 'Active',sort by createdDateTime, limit to the top 1 row, and return the country&quot;
          }
      },
      {
          $sort: 
          {
              'createdDateTime': - 1
          }
      },
      {
          $project: 
          {
              'countryCode': 1
          }
      },
      {
          $limit: 1
      }
  )
  </code></pre>
  <p>If I try to create a view with a &quot;var&quot; in it, like this;</p>
  <p>db.createView(&quot;ip2Locations_vw-lookupcountryfromip&quot;,&quot;ip2Locations&quot;,[
  var ipaddr = 16778237
  db['ip2Locations'].aggregate(</p>
  <p>I get error:</p>
  <pre><code>[Error] SyntaxError: expected expression, got keyword 'var'
  </code></pre>
  <p>In the link I provided above, I think the guy was trying to figure how the $$user-variables work (no example here: <a href="https://docs.mongodb.com/manual/reference/aggregation-variables/" rel="nofollow noreferrer">https://docs.mongodb.com/manual/reference/aggregation-variables/</a>).  That page refers to $let, but never shows how the two work together.  I found one example here: <a href="https://www.tutorialspoint.com/mongodb-query-to-set-user-defined-variable-into-query" rel="nofollow noreferrer">https://www.tutorialspoint.com/mongodb-query-to-set-user-defined-variable-into-query</a> on variables, but not $$variables.  I'm</p>
  <p>db.createView(&quot;ip2Locations_vw-lookupcountryfromip&quot;,&quot;ip2Locations&quot;,[
  db['ip2Locations'].aggregate(
  ...etc...
  &quot;ipFrom&quot;: {
  $lte: $$ipaddr
  }</p>
  <p>I tried ipaddr, $ipaddr, and $$ipaddr, and they all give a variation of this error:</p>
  <pre><code>[Error] ReferenceError: $ipaddr is not defined
  </code></pre>
  <p>In a perfect world, one would be able to do something like:</p>
  <pre><code>   get['ip2Locations_vw-lookupcountryfromip'].find({$let: {'ipaddr': 16778237})
  </code></pre>
  <p>or similar.</p>
  <p>I'm getting that it's possible with Javascript stored in MongoDB (<a href="https://stackoverflow.com/questions/48136206/how-to-use-variables-in-mongodb-query">How to use variables in MongoDB query?</a>), but I'll have to re-read that; seems like some blogs were warning against it.</p>
  <p>I have yet to find a working example using $$user-variables, still looking.</p>