Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB Aggregate$unwind$match using date-我错过了什么?_Mongodb_Aggregation Framework - Fatal编程技术网

MongoDB Aggregate$unwind$match using date-我错过了什么?

MongoDB Aggregate$unwind$match using date-我错过了什么?,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我是MongoDB的新手,我正在尝试使用聚合。我部分地做了我想做的事,但我对约会有一种奇怪的行为 MongoDB信息 版本:2.2.0 操作系统:Windows7 目标 获取“2012-11-22”之后创建的所有评论 让我们举个例子: 数据 db.blogs.save([ { title : "X this is my second title", author : "max", posted : new Date(), pageViews : 10, t

我是MongoDB的新手,我正在尝试使用聚合。我部分地做了我想做的事,但我对约会有一种奇怪的行为

MongoDB信息

版本:2.2.0

操作系统:Windows7

目标

获取“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
    }
} ])

示例1:字符串匹配时确定

从用户“sam”返回所有“评论”:

db.blogs.aggregate( [
   { $unwind: "$comments" },
   { $match: { 'comments.author' : "sam" } },
   { $group: { _id: "$comments" } }
] )
此返回仅用于属性“author”为“sam”的注释


示例2:日期问题?

此聚合(对我而言)与上一个聚合相同,但我没有匹配“author”,而是匹配日期属性“create”:

db.blogs.aggregate( [
   { $unwind: "$comments" },
   { $match: { 
    'comments.create' : {
        $gt: ISODate("2012-11-22T00:00:00Z")
    }
   } },
   { $group: { _id: "$comments" } }
] )
但如果您测试此聚合,您将看到一些注释包含的“创建”日期低于“2012-11-22”。例如,返回ID为“50ac9fdb53a900bcb4be46d9”的注释


我希望只有日期大于“2012-11-22”的评论。。。我想我错过了什么


谢谢你,我的天啊!斯坦尼是对的。现在是十一月而不是十二月

如果我把
2012-12-21T00:00:00Z
放进去,它就开始工作了^^

顺便说一句,正如JohnnyHK所说,这样做可能更好:

db.blogs.aggregate( [
    { $project : { 'comments' : 1 } },
    { $unwind: "$comments" },
    { 
       $match: { 
          'comments.create' : {
                $gt: ISODate("2012-12-21T00:00:00Z")
          }
       } 
    }
])
没有使用
$group
但是使用
$project
我似乎得到了我想要的东西


非常感谢你们的反馈

根据示例代码,ID为50ac9fdb53a900bcb4be46d9的注释的日期为12月,而不是11月:
create:ISODate(“2012-12-20T00:00:00.000Z”)
。看起来一切都在按设计进行;-)。顺便说一句,在
“$comments”
上分组可能不是您想要做的;尝试使用管道中的操作来控制结果中包含的内容。