我正在尝试使用 对 mongo 数据集进行平均计算MongoCollection()::aggregate()
,但是这些函数返回一个 Cursor 对象,我无法弄清楚我做错了什么。
这是数据集内容的示例:
{ word : "word", time : 1234, result : "pass" }
此管道查询在 mongo 控制台中工作:
{"$group" :
{"_id" : "$result",
"meanTime" : {"$avg" :"$time"}
}
}
这是我的代码:
public function getTimes($fields = array('correct','wrong','pass')){
$group = ['$group'=> ["_id" => '$result', "meanTime" => ['$avg' =>'$time']]];
$agg = $this->collection->aggregate(
[$group]
);
return $agg;
}
/*
//This is the var_dump on $agg
object(MongoDB\Driver\Cursor)#82 (2) {
["cursor"]=>
array(17) {
["stamp"]=>
int(0)
["is_command"]=>
bool(false)
["sent"]=>
bool(true)
["done"]=>
bool(false)
["end_of_event"]=>
bool(false)
["in_exhaust"]=>
bool(false)
["has_fields"]=>
bool(false)
["query"]=>
object(stdClass)#76 (0) {
}
["fields"]=>
object(stdClass)#74 (0) {
}
["read_preference"]=>
array(2) {
["mode"]=>
int(1)
["tags"]=>
array(0) {
}
}
["flags"]=>
int(0)
["skip"]=>
int(0)
["limit"]=>
int(0)
["count"]=>
int(2)
["batch_size"]=>
int(0)
["ns"]=>
string(23) "circular.intesavincente"
["current_doc"]=>
object(stdClass)#83 (2) {
["_id"]=>
string(4) "pass"
["meanTime"]=>
float(338)
}
}
["server_id"]=>
int(1)
}
//This is the json_encode output
{}
*/
我试图用array()
构造和简化来编写管道数组[]
,但结果没有改变。我做错了什么?谢谢