title: How can I use a regex variable in a query for MongoDB
body: <p>I would like to make query MongoDB for documents based on a regex expression that I contruct. For e.g
  I have constructed a simple regex as follows that is a combination of a random letter and a random number for in Nodejs</p>
  
  <pre><code>var randnum = Math.floor((Math.random() * 10) + 1);
  var alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z'];
  var randletter = alpha[Math.floor(Math.random() * alpha.length)];
  var val = randletter + randnum + '.*;
  </code></pre>
  
  <p>I have tried various combinations for the regex variable like</p>
  
  <pre><code>var stream = collection.find({"FirstName": /val/).stream();
  &amp;&amp;
  var stream = collection.find({"FirstName": /$val/).stream();
  &amp;&amp;
  var stream = collection.find({"FirstName": {$in : [{$regex:val}]}}).stream()
  &amp;&amp;
  var stream = collection.find({"FirstName": {$in : [{$regex:$val}]}}).stream()
  </code></pre>
  
  <p>None o it seem to work. However when I write the actual regex I get the records for e.g.</p>
  
  <pre><code>var stream = collection.find({"FirstName": /J.*/).stream();
  </code></pre>
  
  <p>Any help will be appreciated.</p>
  
  <p>Thanks
  Ganesh</p>