2017-04-10 64 views
0

我想弄清楚如何编写我的查询,但找不到正确的语法。mongodb查询:在datetime上过滤,在id和count上的组

在下面的集合中,我们有包含0个或更多PageEvents的页面。
我想从一个特定的日期开始将所有的PageEvents,他们分组在他们的唯一ID并计算他们发生了多少次。

  • 条目1具有2页,0 PageEvents
  • 条目2具有1页,1个PageEvent
  • 条目3具有1页,2个PageEvents
  • 条目4具有4页,4个PageEvents(1各)

该查询会给我所有具有给定日期后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

你必须$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 }