mongodb查询:按日期时间筛选,按id和计数分组

mongodb查询:按日期时间筛选,按id和计数分组,mongodb,mongodb-query,Mongodb,Mongodb Query,我试图找出如何编写查询,但找不到正确的语法 在下面的集合中,我们有包含0个或多个PageEvents的页面。 我想获取从特定日期开始的所有页面事件,根据它们的唯一ID对它们进行分组,并计算它们发生的次数 条目1有2页,0页事件 条目2有1页,1页事件 条目3有1页,2页事件 条目4有4个页面,4个页面事件(每个1个) 此查询将提供在给定日期之后包含PageEvents的所有条目: db.Interactions.find({“Pages.PageEvents.DateTime”:{$gte:

我试图找出如何编写查询,但找不到正确的语法

在下面的集合中,我们有包含0个或多个PageEvents的页面。
我想获取从特定日期开始的所有页面事件,根据它们的唯一ID对它们进行分组,并计算它们发生的次数

  • 条目1有2页,0页事件
  • 条目2有1页,1页事件
  • 条目3有1页,2页事件
  • 条目4有4个页面,4个页面事件(每个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
}
在分组之前,必须对所有数组进行排序:

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 }