Sum在php mongodb中不起作用
下面是我的mongodb数据Sum在php mongodb中不起作用,php,mongodb,Php,Mongodb,下面是我的mongodb数据 { "_id" : ObjectId("598be687c54bba05596e347e"), "slotid" : "248x300x250xnewdiv", "request_id" : "07f811ee-a8e3-4d76-9390-49f613f6bb86", "zoneid" : "248", "bidder" : "rubicon", "cpm" : 0.6
{
"_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);
}
我需要按slotid和投标人分组,并求和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
)
)
感谢任何帮助….您的cpm为浮点值,您不能添加浮点值$sum您确定代码中的集合与显示数据的集合相同吗?哪个MongoDB服务器版本?如果某些数据实际上是字符串,服务器可能会有所不同。是的,数据库中也存在相同的集合,mongo db版本为3.4.6,为我正确返回1.2。仅基于上述两个文件。这里有些东西被误解了。是的,你是对的,有可能在mongo db中求和浮点值吗?没有“浮点值”这样的东西。BSON类型有
Double
和Decimal 128
,当然这两种类型都适用于$sum
@neil lunn-如果我将cpm值0.6更改为1,则表示总和有效,并显示总数2@Thiyagu它对其他所有人都非常有效。想想看。这并不是说你是第一个将数据存储为Double
(实际上就是这种类型)的人,所以这不是答案。看起来更可能的是,您的实际文档是字符串,但您在问题中没有这样表示它。