我正在试图弄清楚如何编写查询但找不到正确的语法。
在下面的集合中,我们有包含0个或更多PageEvents的页面 我想从特定日期开始采取所有PageEvents,将它们分组到它们的唯一ID并计算它们发生的次数。
此查询将为我提供在给定日期之后具有PageEvents的所有条目:
db.Interactions.find({ "Pages.PageEvents.DateTime" : { $gte: new ISODate("2017-03-02")} })
这就是我现在正在尝试的......这是行不通的 有人能指出我正确的方向吗?
db.Interactions.aggregate
([
{
$match : {
"Pages.PageEvents.DateTime" : { $gte: new ISODate("2017-03-02")}
}
},
{
$group : {
_id : "$Pages.PageEvents.Name",
count : {$sum: 1}
}
}
])
互动集合:
{
"_id" : LUUID("50795925-2c4c-a54b-8062-249c3a2e5804"),
"_t" : "VisitData",
"OperatingSystem" : {
"_id" : "WinNT"
},
"Pages" : [
{
"DateTime" : ISODate("2017-02-24T14:18:43.951Z"),
"Duration" : 13411,
"VisitPageIndex" : 1
},
{
"DateTime" : ISODate("2017-02-24T14:18:57.362Z"),
"Duration" : 0,
"VisitPageIndex" : 2
}
],
"VisitPageCount" : 2
}
{
"_id" : LUUID("8b492132-926d-194a-9fd5-cf17bc1c9315"),
"_t" : "VisitData",
"OperatingSystem" : {
"_id" : "WinNT"
},
"Pages" : [
{
"DateTime" : ISODate("2017-03-02T14:40:21.579Z"),
"Duration" : 0,
"PageEvents" : [
{
"Name" : "Page error",
"Timestamp" : NumberLong(0),
"Data" : "/",
"DataKey" : "/",
"Text" : "The page '/' failed with message 'No layout'",
"PageEventDefinitionId" : LUUID("526b07b6-a97e-9742-b799-864acdcb0257"),
"DateTime" : ISODate("2017-03-02T14:40:21.617Z"),
"Value" : 0
}
],
"VisitPageIndex" : 1
}
],
"VisitPageCount" : 1
}
{
"_id" : LUUID("2977f06e-cf89-7640-9303-208793d52da7"),
"_t" : "VisitData",
"OperatingSystem" : {
"_id" : "WinNT"
},
"Pages" : [
{
"DateTime" : ISODate("2017-03-02T15:25:35.477Z"),
"Duration" : 0,
"PageEvents" : [
{
"Name" : "Page error",
"Timestamp" : NumberLong(0),
"Data" : "/",
"DataKey" : "/",
"Text" : "The page '/' failed with message 'No layout'",
"PageEventDefinitionId" : LUUID("526b07b6-a97e-9742-b799-864acdcb0257"),
"DateTime" : ISODate("2017-03-02T15:25:35.602Z"),
"Value" : 0
},
{
"Name" : "Long running request",
"Timestamp" : NumberLong(0),
"Data" : "5738",
"DataKey" : "5,739",
"Text" : "Request took 5,739ms to complete",
"PageEventDefinitionId" : LUUID("ff6a6fdc-a96a-3f42-a824-49f9ee741aa9"),
"DateTime" : ISODate("2017-03-02T15:25:37.610Z"),
"Value" : 0
}
],
"VisitPageIndex" : 1
}
],
"VisitPageCount" : 1
}
{
"_id" : LUUID("db3d29db-3936-8045-b1af-72c8f7ca5c1f"),
"_t" : "VisitData",
"OperatingSystem" : {
"_id" : "WinNT"
},
"Pages" : [
{
"DateTime" : ISODate("2017-04-06T11:57:11.132Z"),
"Duration" : 42197,
"PageEvents" : [
{
"Name" : "Page error",
"Timestamp" : NumberLong(0),
"Data" : "/",
"DataKey" : "/",
"Text" : "The page '/' failed with message 'No layout'",
"PageEventDefinitionId" : LUUID("526b07b6-a97e-9742-b799-864acdcb0257"),
"DateTime" : ISODate("2017-04-06T11:57:11.237Z"),
"Value" : 0
}
],
"VisitPageIndex" : 1
},
{
"DateTime" : ISODate("2017-04-06T11:57:53.330Z"),
"Duration" : 132869,
"PageEvents" : [
{
"Name" : "Page error",
"Timestamp" : NumberLong(0),
"Data" : "/",
"DataKey" : "/",
"Text" : "The page '/' failed with message 'No layout'",
"PageEventDefinitionId" : LUUID("526b07b6-a97e-9742-b799-864acdcb0257"),
"DateTime" : ISODate("2017-04-06T14:57:53.331Z"),
"Value" : 0
}
],
"VisitPageIndex" : 2
},
{
"DateTime" : ISODate("2017-04-06T15:00:06.199Z"),
"Duration" : 2765,
"Item" : {
"Version" : 0
},
"PageEvents" : [
{
"Name" : "Page error",
"Timestamp" : NumberLong(0),
"Data" : "/",
"DataKey" : "/",
"Text" : "The page '/' failed with message 'No layout'",
"PageEventDefinitionId" : LUUID("526b07b6-a97e-9742-b799-864acdcb0257"),
"DateTime" : ISODate("2017-04-06T12:00:06.200Z"),
"Value" : 0
}
],
"VisitPageIndex" : 3
},
{
"DateTime" : ISODate("2017-04-06T12:00:08.964Z"),
"Duration" : 0,
"PageEvents" : [
{
"Name" : "Page error",
"Timestamp" : NumberLong(0),
"Data" : "/",
"DataKey" : "/",
"Text" : "The page '/' failed with message 'No layout'",
"PageEventDefinitionId" : LUUID("526b07b6-a97e-9742-b799-864acdcb0257"),
"DateTime" : ISODate("2017-04-06T12:00:08.965Z"),
"Value" : 0
}
],
"VisitPageIndex" : 4
}
],
"VisitPageCount" : 4
}
答案 0 :(得分:0)
在分组之前,您必须$unwind
所有阵列:
db.Interactions.aggregate([{
$match: {
"Pages.PageEvents.DateTime": { $gte: new ISODate("2017-03-02") }
}
}, {
$unwind: "$Pages"
} , {
$unwind: "$Pages.PageEvents"
},{
$group: {
_id: "$Pages.PageEvents.Name",
count: { $sum: 1 }
}
}])
给你:
{ "_id" : "Long running request", "count" : 1 }
{ "_id" : "Page error", "count" : 6 }