MongoDb (Mongoose) query docuemnt comparing two dates using $elemMatch and $and
<p>In my database, I have a table with an object storing dates like this. I have a beginning date and hour (debut in french) and a ending date and hour (fin in french).  </p>

<pre><code>    "disposnibilites" :
     [    
...
      {
            "debut": {
                "$date": "2016-02-15T14:00:00.282Z"
            },
            "fin": {
                "$date": "2016-02-15T19:00:00.282Z"
            },
            "_id": {
                "$oid": "56d48f0bf94a4a642f4d2fc0"
            }
        },
...
       ]
</code></pre>

<p>My goal is to find all the document where the beginning time supplied is before the one in the DB and the end time is after the one in the DB. In Mongoose I run this query : </p>

<pre><code>    { 'disponibilites' :
      {  $elemMatch :  {
          debut : { $lt : new Date("2016-02-15T15:00:00.282Z")},
          fin : { $gt : new Date("2016-02-15T18:00:00.282Z") }
    } 
} };
</code></pre>

<p>This send back all the documents available. Mongoose logs gives this informations : </p>

<pre><code>Mongoose: document.find({ disponibilites: { '$elemMatch': { debut: { '$lt': new Date("Mon, 15 Feb 2016 19:00:00 GMT") }, fin: { '$gt': new Date("Mon, 15 Feb 2016 20:00:00 GMT") } } } }) { fields: undefined }
</code></pre>

<p>Now if I change the day to the 17th (where there are no entries in the DB for that day), it gives the same result : </p>

<pre><code>    { 'disponibilites' :
      {  $elemMatch :  {
          debut : { $lt : new Date("2016-02-17T15:00:00.282Z")},
          fin : { $gt : new Date("2016-02-17T18:00:00.282Z") }
      } 
    }
</code></pre>

<p>Mongoose logs data : </p>

<pre><code>Mongoose: document.find({ disponibilites: { '$elemMatch': { debut: { '$lt': new Date("Mon, 17 Feb 2016 19:00:00 GMT") }, fin: { '$gt': new Date("Mon, 17 Feb 2016 20:00:00 GMT") } } } }) { fields: undefined }
</code></pre>

<p>Eventually I will need to have something more complex like this : </p>

<pre><code>{ 'disponibilites' :
   { $and : [
    {  $elemMatch :  {
        debut : { $lt : new Date("2016-02-17T15:00:00.282Z")},
        fin : { $gt : new Date("2016-02-17T18:00:00.282Z") }
    },
    {  $elemMatch :  {
        debut : { $lt : new Date("2016-02-18T15:00:00.282Z")},
        fin : { $gt : new Date("2016-02-18T18:00:00.282Z") }
    } 
   ]
   }
}
</code></pre>

<p>For now I want to understand why MongoDb can't compare dates properly ?</p>