使用日期MongoDB聚合$ unwind $ match - 我错过了什么?

Max*_*nce 6 mongodb aggregation-framework

我是MongoDB的新手,我正在尝试使用聚合.我部分地做了我正在寻找的但是我对日期有一种奇怪的行为.

MongoDB信息

版本:2.2.0

操作系统:Windows 7

目的

获取"2012-11-22"之后创建的所有评论

我们来举个例子:

数据

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
    }
} ])
Run Code Online (Sandbox Code Playgroud)

示例1:确定字符串匹配

返回用户'sam'的所有"评论":

db.blogs.aggregate( [
   { $unwind: "$comments" },
   { $match: { 'comments.author' : "sam" } },
   { $group: { _id: "$comments" } }
] )
Run Code Online (Sandbox Code Playgroud)

这只返回评论属性'author'是'sam'.


例2:日期问题?

这个聚合(对我来说)与前一个聚合相同,但我没有匹配'author',而是匹配date属性'create':

db.blogs.aggregate( [
   { $unwind: "$comments" },
   { $match: { 
    'comments.create' : {
        $gt: ISODate("2012-11-22T00:00:00Z")
    }
   } },
   { $group: { _id: "$comments" } }
] )
Run Code Online (Sandbox Code Playgroud)

但是,如果您测试此聚合,您会看到某些评论包含的"创建"日期低于"2012-11-22".例如,返回ID为"50ac9fdb53a900bcb4be46d9"的注释.


我希望只有日期大于'2012-11-22'的评论......我想我错过了一些东西......

谢谢

Max*_*nce 5

我的天啊!斯坦尼是对的.这是十一月而不是十二月......

如果我把2012-12-21T00:00:00Z它工作...... ^^

顺便说一句,正如JohnnyHK所说,以这种方式进行操作可能更好:

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

没有使用$group但使用$project它似乎我得到了我正在寻找的东西.

非常感谢您的反馈!