MongoDB使用日期汇总$ unwind $ match-我错过了什么? [英] MongoDB Aggregate $unwind $match using date - what did I miss?

查看:68
本文介绍了MongoDB使用日期汇总$ unwind $ match-我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB的新手,我正在尝试使用聚合.我部分地做了我要寻找的东西,但是我有一个奇怪的约会行为.

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信息

版本:2.2.0

Version : 2.2.0

操作系统:Windows 7

Operating System : Windows 7

客观

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

Get all comments created after '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"的所有评论":

Return all 'comments' from user 'sam' :

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

这仅返回属性作者"为"sam"的注释.

This return only comments where property 'author' is 'sam'.

示例2:发布日期?

(对于我而言)此聚合与上一个聚合相同,但与日期属性创建"匹配,而不是匹配作者":

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" } }
] )

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

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.

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

I would expect only comments with dates greater than '2012-11-22'... I guess I missed something...

谢谢

推荐答案

我的天哪!斯坦尼是对的. 11月,而不是12月...

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

如果我放2012-12-21T00:00:00Z,它就可以正常工作... ^^

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

顺便说一句,正如JohnnyHK所说的那样,最好进行这种操作:

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")
          }
       } 
    }
])

不使用$group而是使用$project似乎可以得到想要的东西.

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

非常感谢您的反馈!

这篇关于MongoDB使用日期汇总$ unwind $ match-我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆