title: How to create a query on a Date field in MongoDB using mongoose?
body: <p>I am trying to query a collection in Mongo database, to get all record with <code>Time</code> field in a date range. <code>Time</code> is defined as Date in database.<br/>
  My environment: Node.js, Express, Jade, javascript.</p>
  
  <p>This is the javascript code:</p>
  
  <pre><code>var query = {};
  var timeQuery = {};
  timeQuery["$lt"] = new Date().toISOString();
  query["Time"] = timeQuery;
  console.log(query);
  db.model('testruns').find(query).exec(function (err, testruns) {
      console.log(testruns.length);
      // doing something
  });
  </code></pre>
  
  <p>the result printed to console: </p>
  
  <blockquote>
    <p>{ Time: { '$lt': '2014-10-30T15:04:39.256Z' } }<br/>
    0</p>
  </blockquote>
  
  <p>The query returns 0 results (there should be more)</p>
  
  <p>By the way... Running date queries from RoboMongo returns results, just the wrong ones. for example:</p>
  
  <pre><code>db.testruns.find({Time : {"$gte" : new Date("2014-10-30T15:13:37.199Z")}})
  </code></pre>
  
  <p>returns all records.</p>
  
  <p>What I tried:
  <a href="https://stackoverflow.com/questions/11973304/mongodb-mongoose-querying-at-a-specific-date">This one</a>, <a href="https://stackoverflow.com/questions/12249223/mongoose-query-nested-documents-greater-or-less-a-certain-date">that one</a>, <a href="https://stackoverflow.com/questions/10182680/how-to-query-based-on-a-date-field-in-the-latest-version-of-mongoose">another one</a>, mongoose documentation of course, and many more results from google.</p>
  
  <p>Most of them give the same answer, none of them works for me. HELP!</p>