title: $where mongo operator work around in meteorjs
body: <p>
  I have a MongoDB query that works perfectly in MongoDB shell </p>
  
  <pre class="lang-js prettyprint-override"><code> db.collection.find({ $where: 
  function(){
      var num = this.numbers;
      function isInList(numbers,matchArray) 
          {
              var reducedNums = numbers.filter(function(num) {
              return matchArray.indexOf(num) !== -1
              });
              if (reducedNums.length == 7){
                  return true;
              } else {
                  return false;
              }
          }
      return isInList(num, [ 28 ,5, 17, 47, 1, 24, 37, 19, 4, 3 ] );
      }
  }
  );
  </code></pre>
  
  <p>How can I implement this in MeteorJS what is the work around?</p>
  
  <p>Explanations as per request: 
  I need a function in the serverside, does not matter if it works in the client side.
  The function has to match the given array with the numbers field on the document and tell me how many numbers in the array are matching and the document ID or something like this : </p>
  
  <pre class="lang-js prettyprint-override"><code>if (match.length == 7){
                  return "DOCUMENT";
              } else {
                  return false;
              }
  </code></pre>
  
  <p>Tried this:
  Added the code in (meteor is server):</p>
  
  <pre class="lang-js prettyprint-override"><code>console.log(Tickets.find({ $where: 
  function(){
          var num = this.numbers;
          function isInList(numbers,matchArray) 
              {
                  var reducedNums = numbers.filter(function(num) {
                  return matchArray.indexOf(num) !== -1
                  });
                  if (reducedNums.length == 7){
                      return true;
                  } else {
                      return false;
                  }
              }
          return isInList(num, [ 28 ,5, 17, 47, 1, 24, 37, 19, 4, 3 ] );
          }
      }
  ))
  </code></pre>
  
  <p>output was like this:</p>
  
  <pre class="lang-js prettyprint-override"><code> I20140623-17:15:02.690(2)? { _mongo:
   I20140623-17:15:02.910(2)?    { _connectCallbacks: [ [Function] ],
   I20140623-17:15:02.911(2)?      _observeMultiplexers: {},
   I20140623-17:15:02.912(2)?      _onFailoverHook: { nextCallbackId: 0, callbacks: {} },
   I20140623-17:15:02.913(2)?      _docFetcher: { _mongoConnection: [Circular], _callbacksForCacheKey: {} },
   I20140623-17:15:02.914(2)?      _oplogHandle:
   ...
   ...
   ...
   I20140623-17:15:03.025(2)?      selector: { '$where': [Function] },
   I20140623-17:15:03.027(2)?      options: { transform: null } },
   I20140623-17:15:03.030(2)?   _synchronousCursor: null }
   =&gt; Meteor server restarted
  </code></pre>
  
  <p>When I do .fetch() all the documents will print out which should not happen this code works perfectly in mondodb shell.</p>
  
  <p><strong>Solution</strong> thank's to @AndrewMao:
  1st solution:
  By adding the .toString() in the end of the function the .toString() as I tested works perfectly in NodeJS</p>
  
  <p>ex:</p>
  
  <pre class="lang-js prettyprint-override"><code>Tickets.find({'$where':     function(){
      var num = this.numbers;
      function isInList(numbers,matchArray) 
          {
              var reducedNums = numbers.filter(function(num) {
              return matchArray.indexOf(num) !== -1
              });
              if (reducedNums.length == 7){
                  return true;
              } else {
                  return false;
              }
          }
      return isInList(num, [ 28 ,5, 17, 47, 1, 24, 37, 19, 4, 3 ] );
      }.toString();
  }).fetch();
  </code></pre>
  
  <p>2nd solution: This is if you want to add you custom array or something else in the query:</p>
  
  <p>You have to pass your function as a string in one line, also you can add other string in it as I did with the random array. Look at the function call (isInList(num,['+ random +']);}') in the example below.</p>
  
  <p>ex:</p>
  
  <pre class="lang-js prettyprint-override"><code> var random = [ 19 ,3, 14, 24, 2, 38, 48, 19, 4, 3 ];
  
   Tickets.find({'$where': 'function(){var num = this.numbers; function isInList(numbers,matchArray) {var reducedNums = numbers.filter(function(num) { return matchArray.indexOf(num) !== -1  }); if (reducedNums.length == 7){ return true;} else {  return false;   }}return isInList(num,['+ random +']);}'}).fetch();
  </code></pre>