title: MongoDB Aggregate $unwind $match using date - what did I miss?
body: <p>I'm new in MongoDB and I'm trying to work with aggregations. I partially do what I'm looking for but I have a strange behavior with dates. </p>
  
  <p><strong>MongoDB info</strong> </p>
  
  <blockquote>
    <p>Version : 2.2.0</p>
    
    <p>Operating System : Windows 7</p>
  </blockquote>
  
  <p><strong>Objective</strong> </p>
  
  <blockquote>
    <p>Get all comments created after '2012-11-22'</p>
  </blockquote>
  
  <p>Let's get an example :</p>
  
  <p><strong>Data</strong></p>
  
  <pre><code>db.blogs.save([ {
      title : "X this is my second title",
      author : "max",
      posted : new Date(),
      pageViews : 10,
      tags : [ "good", "nice" ],
      comments : [ {
          "_id" : ObjectId("50ac9fdb53a900bcb4be46d9"),
          author : "john",
          text : "pretty awesome",
          create : ISODate("2012-12-20T00:00:00.000Z")
      }, {
          "_id" : ObjectId("50ac9fd003a900bcb4be46d9"),
          author : "sam",
          text : "this is bad",
          create : ISODate("2012-12-22T00:00:00.000Z")
      } ],
      other : {
          foo : 5
      }
  }, {
      title : "X this is my title",
      author : "bob",
      posted : new Date(),
      pageViews : 5,
      tags : [ "fun", "good", "fun" ],
      comments : [ {
          "_id" : ObjectId("50ac55db53a900bcb4be46d9"),
          author : "matthieu",
          text : "bof bof",
          create : ISODate("2012-12-21T00:00:00.000Z")
      }, {
          "_id" : ObjectId("50ac55db53a900bcb4b226d9"),
          author : "sam",
          text : "this s bad",
          create : ISODate("2012-12-22T00:00:00.000Z")
      } ],
      other : {
          foo : 6
      }
  }, {
      title : "X NEW ELEMENT",
      author : "emil",
      posted : new Date(),
      pageViews : 33,
      tags : [ "bad", "hehe", "cool", "nice" ],
      comments : [ {
          "_id" : ObjectId("50ac55db531100bcb4b226d9"),
          author : "emilie",
          text : "could be better",
          create : ISODate("2012-12-21T00:00:00.000Z")
      }, {
          "_id" : ObjectId("50ac55db101100bcb4b226d9"),
          author : "samuel",
          text : "maybe a good one",
          create : ISODate("2012-12-20T00:00:00.000Z")
      } ],
      other : {
          foo : 9
      }
  }, {
      title : "X Y NEW ELEMENT",
      author : "marc",
      posted : new Date(),
      pageViews : 33,
      tags : [ "bad", "hehe", "cool", "nice" ],
      comments : [ {
          "_id" : ObjectId("50ac55db101100bcb4baa6d9"),
          author : "sam",
          text : "hehe",
          create : ISODate("2012-11-20T00:00:00.000Z")
      }, {
          "_id" : ObjectId("50ac55db101ab0bcb4baa6d9"),
          author : "daniel",
          text : "yeehhhh hoho",
          create : ISODate("2012-11-23T00:00:00.000Z")
      } ],
      other : {
          foo : 9
      }
  } ])
  </code></pre>
  
  <hr>
  
  <p><strong>Example 1 : OK with strings matching</strong></p>
  
  <p>Return all 'comments' from user 'sam' :</p>
  
  <pre><code>db.blogs.aggregate( [
     { $unwind: "$comments" },
     { $match: { 'comments.author' : "sam" } },
     { $group: { _id: "$comments" } }
  ] )
  </code></pre>
  
  <p>This return only comments where property 'author' is 'sam'.</p>
  
  <hr>
  
  <p><strong>Example 2 : issue with dates ?</strong></p>
  
  <p>this aggregation is (for me) the same as the previous one but instead of matching 'author', I match the date property 'create' :</p>
  
  <pre><code>db.blogs.aggregate( [
     { $unwind: "$comments" },
     { $match: { 
      'comments.create' : {
          $gt: ISODate("2012-11-22T00:00:00Z")
      }
     } },
     { $group: { _id: "$comments" } }
  ] )
  </code></pre>
  
  <p>But if you test this aggregation, you will see that some comments contains 'create' dates lower than '2012-11-22'. For instance, comment with ID '50ac9fdb53a900bcb4be46d9' is returned.</p>
  
  <hr>
  
  <p>I would expect only comments with dates greater than '2012-11-22'... I guess I missed something...</p>
  
  <p>Thank you</p>