2016-11-09 41 views
1

我嘗試前進,並且我認爲我還沒到目前爲止。再次爲您提供幫助。我只得到一個包含請求的日期結果(請參閱編輯)。我不知道我怎麼能在一天之內得到結果。我已經調整了包括所有必需文件的比賽條件。我不知道你爲什麼要把$放在第一位。

db.collection.aggregate([ 
{ "$match": {"CollectionTime": { $gte: ISODate("2016-11- 10T00:00:00.000Z")},"Rx Rate": { $exists: true }}}, 
{ "$group": { 
"_id": "$ResourceName", 
DeviceName: { $first: "$DeviceName" }, 
ResourceName: { $first: "$ResourceName" }, 
Date: { $first: "$CollectionTime" }, 
AVGRxRATE: { $avg: "$Rx Rate" }, 
AVGTxRATE: { $avg: "$Tx Rate" }, 
MaxRxRATE: { $max: "$Rx Rate" }, 
MaxTxRATE: { $max: "$Tx Rate" } 
} 
}, 
{ 
$project: { 
_id: 0, 
DeviceName: 1, 
ResourceName: 1, 
Date: { $dateToString: { format: "%Y-%m-%d", date: "$Date" } }, 
AVGRxRATE: 1, 
AVGTxRATE: 1, 
MaxRxRATE: 1, 
     MaxTxRATE: 1 
    } 
    } 
    ]) 

回答

0

我認爲這是你正在尋找的 - 你有一些不必要的步驟在你的聚合管道。

$unwind用於將數組拆分爲單獨的文檔,而$project用於整形或重命名字段。

db.u2000.aggregate([ 
    { "$match": {"CollectionTime": { $gte: ISODate("2016-11-05T00:00:00.000Z")}}}, 
    { "$group": { 
      "_id": null, 
      AVGRxRATE: { $avg: "$Rx Rate" }, 
      AVGTxRATE: { $avg: "$Tx Rate" }, 
      MaxRxRATE: { $max: "$Rx Rate" }, 
      MaxTxRATE: { $max: "$Tx Rate" } 
     } 
    } 
]) 

這將導致以下爲你的「一」的公文包,但應對多個文檔工作:

{ 
    "_id" : null, 
    "AVGRxRATE" : 522, 
    "AVGTxRATE" : 176, 
    "MaxRxRATE" : NumberLong(522), 
    "MaxTxRATE" : NumberLong(176) 
} 

*編輯*

運行以下彙總得到確切的輸出你想要...注意,這並不是真的在任何東西上進行分組,而更多的是對一個文檔的操縱,以你要求的方式來塑造它,因爲我們是通過null進行分組的。

db.u2000.aggregate([ 
    { "$match": {"CollectionTime": { $gte: ISODate("2016-11-05T00:00:00.000Z")}}}, 
    { "$group": { 
      "_id": null, 
      DeviceName: { $first: "$DeviceName" }, 
      ResourceName: { $first: "$ResourceName" }, 
      Date: { $first: "$CollectionTime" }, 
      AVGRxRATE: { $avg: "$Rx Rate" }, 
      AVGTxRATE: { $avg: "$Tx Rate" }, 
      MaxRxRATE: { $max: "$Rx Rate" }, 
      MaxTxRATE: { $max: "$Tx Rate" } 
     } 
    }, 
    { 
     $project: { 
      _id: 0, 
      DeviceName: 1, 
      ResourceName: 1, 
      Date: { $dateToString: { format: "%Y-%m-%d", date: "$Date" } }, 
      AVGRxRATE: 1, 
      AVGTxRATE: 1, 
      MaxRxRATE: 1, 
      MaxTxRATE: 1 
     } 
    } 
]) 

結果:

{ 
    "DeviceName" : "OLT", 
    "ResourceName" : "OLT/Frame:0/Slot:19/Port:0", 
    "Date" : "2016-11-07", 
    "AVGRxRATE" : 522, 
    "AVGTxRATE" : 176, 
    "MaxRxRATE" : NumberLong(522), 
    "MaxTxRATE" : NumberLong(176) 
} 
+0

感謝clarification.Every 5萬,我收到了文件,我的目標是讓白天(AVG,最大值)聚合每個的ressource名稱: 設備名稱:「OLT」 資源名稱:「OLT /幀:0 /插槽:19 /端口:0」 日期:2016-11-07 AVG Tx率:NumberLong(「176」) AVG Rx評分: NumberLong(「522」) MAX Tx Rate:NumberLong(「176」) MAX Rx Rate:NumberLong(「522」) 如何添加日期和信息,如設備名稱? – lalarita

+0

請問我如何添加其他信息並更改日期格式?@dyouberg – lalarita

+0

請參閱編輯上面的答案 – dyouberg