MongoDB Aggregate $unwind $match using date - what did I miss?

aggregation-framework, mongodb

Solution

Ho my god! Stennie is right. It's November and not December...

If I put `2012-12-21T00:00:00Z` it's working... ^^

Btw, as JohnnyHK said it's maybe better to do the operation this way :

db.blogs.aggregate( [
    { $project : { 'comments' : 1 } },
    { $unwind: "$comments" },
    { 
       $match: { 
          'comments.create' : {
                $gt: ISODate("2012-12-21T00:00:00Z")
          }
       } 
    }
])

Without using `$group` but using `$project` it seems that I get what I'm looking for.

Thank you very much both for your feedbacks!

Problem

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. MongoDB info Version : 2.2.0 Operating System : Windows 7 Objective Get all comments created after '2012-11-22' Let's get an example : Data ``` 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 } } ]) ``` Example 1 : OK with strings matching Return all 'comments' from user 'sam' : ``` db.blogs.aggregate( [ { $unwind: "$comments" }, { $match: { 'comments.author' : "sam" } }, { $group: { _id: "$comments" } } ] ) ``` This return only comments where property 'author' is 'sam'. Example 2 : issue with dates ? this aggregation is (for me) the same as the previous one but instead of matching 'author', I match the date property 'create' : ``` db.blogs.aggregate( [ { $unwind: "$comments" }, { $match: { 'comments.create' : { $gt: ISODate("2012-11-22T00:00:00Z") } } }, { $group: { _id: "$comments" } } ] ) ``` 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. I would expect only comments with dates greater than '2012-11-22'... I guess I missed something... Thank you

Original source