下面是我的MongoDB的數據,和不是在PHP的MongoDB工作
{
"_id" : ObjectId("598be687c54bba05596e347e"),
"slotid" : "248x300x250xnewdiv",
"request_id" : "07f811ee-a8e3-4d76-9390-49f613f6bb86",
"zoneid" : "248",
"bidder" : "rubicon",
"cpm" : 0.6,
"width" : NumberInt("300"),
"height" : NumberInt("250"),
"is_win" : NumberInt("1"),
"is_fail" : NumberInt("0"),
"reponsetime" : NumberInt("294"),
"requestdate" : "2017-08-10 04:52:25",
"responsedate" : "2017-08-10 04:52:26"
},
{
"_id" : ObjectId("598be687c54bba05596e3480"),
"slotid" : "248x300x250xnewdiv",
"request_id" : "07f811ee-a8e3-4d76-9390-49f613f6bb86",
"zoneid" : "247",
"bidder" : "rubicon",
"cpm" : 0.6,
"width" : NumberInt("468"),
"height" : NumberInt("60"),
"is_win" : NumberInt("1"),
"is_fail" : NumberInt("0"),
"reponsetime" : NumberInt("299"),
"requestdate" : "2017-08-10 04:52:25",
"responsedate" : "2017-08-10 04:52:26"
}
我需要使用PHP來從MongoDB的數據,下面是我的PHP代碼
$res = array(
array(
'$group' => array(
"_id" => array("slotid" => '$slotid',"bidder" => '$bidder'),"total" => array('$sum' => '$cpm'),
),
),
);
$db = (new MongoDB\Client)->database;
$resbids = $db->responsebids->aggregate($res);
foreach($resbids as $res)
{
print_r($res);
}
我需要GROUP BY slotid和bidder和sum cpm值,該組運行正常,但cpm總是顯示零,這個代碼有什麼錯誤?下面 是我的樣本結果,
MongoDB\Model\BSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[_id] => MongoDB\Model\BSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[slotid] => 248x300x250xnewdiv
[bidder] => rubicon
)
)
[total] => 0
)
)
Anyhelp讚賞....
你確定你有相同的集合中的代碼顯示數據來自哪裏?哪個MongoDB服務器版本?如果某些數據實際上是字符串,服務器可能會有所作爲。 –
是的同樣的集合存在於db中,mongo db版本是3.4.6 – Thiyagu
爲我正確返回1.2。只是基於上述兩個文件。有些東西在這裏被歪曲了。 –