title: convert string to date in mongodb?
body: <p>I am using <code>find</code> to get the result then I'm applying <code>Array.prototype.filter</code> in javascript to filter the dates in between 2 given date strings in format <code>dd-mm-yyyy</code>. </p>
  
  <pre><code>let query = {};
  req.query.query &amp;&amp; (query.name = new RegExp(req.query.query, 'i'));
  req.query.gender &amp;&amp; (query.gender = new RegExp(`^${req.query.gender}$`, 'i'));
  Student.find(query, { roll: 1, name: 1, _id: 0, email: 1, DOB: 1, gender: 1 }, (err, students) =&gt; {
      if (err) {
          next(err);
      }
      else {
          if (req.query.DOB_from &amp;&amp; req.query.DOB_to) {
              let from = toDate(req.query.DOB_from);
              let to = toDate(req.query.DOB_to)
              res.send(filterBetweenDates(students, from, to));
          }
          else {
              res.send(students)
          }
      }
  });
  
  
  /**
   * Returns time val in mil secs from a date string in Indian format i.e dd-mm-yyyy
   * @param {string} dateStr 
   */
  function toDate(dateStr) {
      let a = dateStr.split('-').map(Number);
      let d = new Date(a[2], a[1] - 1, a[0]);
      return d.getTime();
  }
  
  /**
   * Filters the result which matches the date
   * @param {Student1[]} students resultant students array
   * @param {Number} from time val in millisecs(from)
   * @param {Number} to time val in millisecs(from)
   */
  function filterBetweenDates(students, from, to) {
      return students.filter(({ DOB }) =&gt; {
          return (from &lt;= toDate(DOB)) &amp;&amp; (to &gt;= toDate(DOB));
      });
  }
  </code></pre>
  
  <p>The issue I'm facing is that the dates are in <code>dd-mm-yyyy</code> format saved as a <code>string</code> And also the input is in the same format as string. So, I'm first applying other queries and then filtering according to the dates. Is there a way I can do this in the query itself?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Sample Collection</p>
  
  <pre><code>    [
      {
          "name": "vib",
          "roll": 413,
          "email": "abc@example.com",
          "DOB": "25-07-1997",
          "gender": "Male"
      }
      {
          "name":"abc",
          "roll":123,
          "email": "abc@xyz.com",
          "DOB": "07-11-2000",
          "gender": "Female"
      }
  ]
  </code></pre>