title: How to use an external variable in MongoDB 'where' query via Javascript?
body: <p>I have a MongoDB query that searches all properties for a value defined in the search variable. It works the following way:</p>
  
  <pre><code> db.collection.findOne({
                  $where: function() {
                      var search = 'searchstring';
                      for (var key in this) {
                          if (this[key] === search) {
                            return true;
                          }
                          return false;
                      }
                  }
              });
  </code></pre>
  
  <p>However, I would like to define the search variable outside the query. </p>
  
  <p>But when I do so, I get an error that it is not referenced (i.e. scoping issue): </p>
  
  <pre><code>"ReferenceError: search is not defined near '[key] === search
  </code></pre>
  
  <p>How can I use or pass the variable to the query filter?</p>