我有以下集合:mongodb的聚合子查詢:MongoDB的PHP的適配器
**S_SERVER** – **S_PORT** – **D_PORT** – **D_SERVER** – **MBYTES**
L0T410R84LDYL – 2481 – 139 – MRMCRUNCHAPP – 10
MRMCASTLE – 1904 – 445 – MRMCRUNCHAPP – 25
MRMXPSCRUNCH01 – 54769 – 445 – MRMCRUNCHAPP - 2
MRMCASTLE – 2254 – 139 – MRMCRUNCHAPP - 4
MRMCASTLE – 2253 – 445 – MRMCRUNCHAPP -35
MRMCASTLE – 987 – 445 – MRMCRUNCHAPP – 100
MRMCASTLE – 2447 – 445 – MRMCRUNCHAPP – 12
L0T410R84LDYL – 2481 – 139 – MRMCRUNCHAPP - 90
MRMCRUNCHAPP – 61191 – 1640 – OEMGCPDB – 10
首先,我需要前30 S_SERVER按照從每個S_SERVER轉移總字節。這是我能夠獲得與下面的查詢:
$sourcePipeline = array(
array(
'$group' => array(
'_id' => array('sourceServer' => '$S_SERVER'),
'MBYTES' => array('$sum' => '$MBYTES')
),
),
array(
'$sort' => array("MBYTES" => -1),
),
array(
'$limit' => 30
)
);
$sourceServers = $collection->aggregate($sourcePipeline);
我還需要30強D端口按每個D端口個人S_SERVER上傳輸的總字節。我通過從上面的服務器結果運行for循環並逐個爲每個S_SERVER逐個獲取它們來完成此操作。
$targetPortPipeline = array(
array(
'$project' => array('S_SERVER' => '$S_SERVER', 'D_PORT' => '$D_PORT', 'MBYTES' => '$MBYTES')
),
array(
'$match' => array('S_SERVER' => S_SERVER(find from above query, passed one by one in for loop)),
),
array(
'$group' => array(
'_id' => array('D_PORT' => '$D_PORT'),
'MBYTES' => array('$sum' => '$MBYTES')
),
),
array(
'$sort' => array("MBYTES" => -1),
),
array(
'$limit' => $limit
)
);
$targetPorts = $collection->aggregate($targetPortPipeline);
但是這個過程花費了太多時間。我需要一種有效的方式來在相同的查詢中獲得所需的結果。我知道我使用Mongodb的PHP適配器來實現這一點。你也可以讓我知道javascript格式的聚合函數。我將它轉換成php。
非常感謝您的想法。它主要用於我。我通過聚合獲得了大部分結果,並在PHP結束時處理了一些結果。一定會與大家分享答案。 –