Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 使用复合键聚合_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb 使用复合键聚合

Mongodb 使用复合键聚合,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我正在尝试聚合一些数据,并按时间间隔对其进行分组,如果您愿意,还将维护一个子类别。我希望能够绘制出这些数据,这样我将有多个不同的行对应于调用的每个办公室。X轴为时间间隔,Y轴为平均环时间 我的数据如下所示: Calls: [{ created: ISODate(xyxyx), officeCalled: 'ABC Office', answeredAt: ISODate(xyxyx) }, { created: ISODate(xyxyx),

我正在尝试聚合一些数据,并按时间间隔对其进行分组,如果您愿意,还将维护一个子类别。我希望能够绘制出这些数据,这样我将有多个不同的行对应于调用的每个办公室。X轴为时间间隔,Y轴为平均环时间

我的数据如下所示:

 Calls: [{
    created: ISODate(xyxyx), 
    officeCalled: 'ABC Office', 
    answeredAt: ISODate(xyxyx)
    },
    {
    created: ISODate(xyxyx), 
    officeCalled: 'Office 2', 
    answeredAt: ISODate(xyxyx)
    },
    {
    created: ISODate(xyxyx), 
    officeCalled: 'Office 3', 
    answeredAt: ISODate(xyxyx)
    }];
[{"_id":TimeInterval1,"calls":[{"office":"ABC Office","ringTime":30720},
            {"office":"Office2","ringTime":3070}]}, 
 {"_id":TimeInterval2,"calls":[{"office":"Office1","ringTime":1125},
            {"office":"ABC Office","ringTime":15856}]}] 
我的目标是让我的电话按时间间隔(30分钟/1小时/1天)和所呼叫的办公室分组。因此,当我的聚合完成时,我将查找如下数据:

 Calls: [{
    created: ISODate(xyxyx), 
    officeCalled: 'ABC Office', 
    answeredAt: ISODate(xyxyx)
    },
    {
    created: ISODate(xyxyx), 
    officeCalled: 'Office 2', 
    answeredAt: ISODate(xyxyx)
    },
    {
    created: ISODate(xyxyx), 
    officeCalled: 'Office 3', 
    answeredAt: ISODate(xyxyx)
    }];
[{"_id":TimeInterval1,"calls":[{"office":"ABC Office","ringTime":30720},
            {"office":"Office2","ringTime":3070}]}, 
 {"_id":TimeInterval2,"calls":[{"office":"Office1","ringTime":1125},
            {"office":"ABC Office","ringTime":15856}]}] 
在过去的几个小时里,我一直在四处闲逛,我能够聚合我的数据,但我还没有弄清楚如何正确地对数据进行分组,以便我可以将每个时间间隔与office数据一起使用。这是我的最新代码:

Call.aggregate([
{$match: {
    $and: [
        {created: {$exists: 1}}, 
        {answeredAt: {$exists: 1}}]}},
{$project: {    created: 1, 
            officeCalled: 1, 
        answeredAt: 1,  
        timeToAns: {$subtract: ["$answeredAt", "$created"]}}},

{$group: {_id: {"day": {"$dayOfYear": "$created"},

        "hour": {
            "$subtract": [
                {"$hour" : "$created"}, 
                {"$mod": [ {"$hour": "$created"}, 2]}
                ]
        }, 
                "officeCalled": "$officeCalled"

 }, 
    avgRingTime: {$avg: '$timeToAns'}, 
    total: {$sum: 1}}},

{"$group": {
        "_id": "$_id.day", 
        "calls": {
            "$push": {
                "office": "$_id.officeCalled", 
                "ringTime": "$avgRingTime"

            }, 

        }
    }}, 
 {$sort: {_id: 1}}


]).exec(function(err, results) {
    //My results look like this

     [{"_id":118,"calls":[{"office":"ABC Office","ringTime":30720}, 
       {"office":"Office 2","ringTime":31384.5},
       {"office":"Office 3","ringTime":7686.066666666667},...];

      });
这只是不太明白…我得到我的数据,但它只是按天细分。我拍摄的时间间隔不是2小时。如果我做错了,请告诉我——我对聚合非常陌生,因此非常感谢您的帮助


谢谢

您真正需要做的就是在最后一组中包含您想要的
\u id
值的两个部分。不知道你为什么认为只引用一个字段

另外,“松开
$project
”,因为它只是浪费了周期和处理,而您可以在第一次尝试时直接在
$group
中使用:

Call.aggregate(
[
{“$match”:{
“已创建”:{“$exists”:1},
“answeredAt”:{“$exists”:1}
}},
{“$组”:{
“_id”:{
“日”:{“$dayOfYear”:“$created”},
“小时”:{
“$subtract”:[
{“$hour”:“$created”},
{“$mod”:[{“$hour”:“$created”},2]}
]
}, 
“officeCalled”:“$officeCalled”
}, 
“avgRingTime”:{
“$avg”:{“$subtract”:[“$answeredAt”,“$created”]}
}, 
“总计”:{“$sum”:1}
}},
{“$组”:{
“_id”:{
“日”:“$\u id.day”,
“小时”:“$\u id.hour”
}, 
“电话”:{
“$push”:{
“办公室”:“$\u id.officeCalled”,
“铃声时间”:“$avgRingTime”
}, 
},
“总计”:{“$sum”:“$total”}
}},
{“$sort”:{“\u id”:1}
]
).exec(函数(错误、结果){
});

还要注意完全省略了
$和
。不需要这样做,因为所有的MongoDB查询参数都已经是“AND”条件,除非另有特别说明。只要坚持简单的原则。这应该很简单。

谢谢你,伙计!你是对的…它很简单,我想我一直希望它更复杂,所以我感谢你的帮助。干杯