MongoDB Aggregate $unwind $match 使用日期 - 我错过了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13563002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB Aggregate $unwind $match using date - what did I miss?
提问by Maxence
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 的新手,我正在尝试使用聚合。我部分地做我正在寻找的东西,但我对日期有一种奇怪的行为。
MongoDB info
MongoDB 信息
Version : 2.2.0
Operating System : Windows 7
版本:2.2.0
操作系统:Windows 7
Objective
客观的
Get all comments created after '2012-11-22'
获取“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
示例 1:字符串匹配正常
Return all 'comments' from user 'sam' :
返回用户 'sam' 的所有 'comments' :
db.blogs.aggregate( [
{ $unwind: "$comments" },
{ $match: { 'comments.author' : "sam" } },
{ $group: { _id: "$comments" } }
] )
This return only comments where property 'author' is 'sam'.
此返回仅评论属性 'author' 为 'sam' 的地方。
Example 2 : issue with dates ?
示例 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" } }
] )
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”的“创建”日期。例如,返回 ID 为“50ac9fdb53a900bcb4be46d9”的评论。
I would expect only comments with dates greater than '2012-11-22'... I guess I missed something...
我希望只有日期大于“2012-11-22”的评论......我想我错过了一些东西......
Thank you
谢谢
采纳答案by Maxence
Ho my god! Stennie is right. It's Novemberand not December...
天啊!斯坦尼是对的。是十一月不是十二月
If I put 2012-12-21T00:00:00Z
it's working... ^^
如果我把2012-12-21T00:00:00Z
它工作...... ^^
Btw, as JohnnyHK said it's maybe better to do the operation this way :
顺便说一句,正如 JohnnyHK 所说,以这种方式进行操作可能会更好:
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.
不使用$group
但使用$project
它似乎我得到了我正在寻找的东西。
Thank you very much both for your feedbacks!
非常感谢您的反馈!