我想将光标转换为数组,所以我看到了光标结果,为此,当我使用amray of mongodb然后显示此错误
"致命错误:调用未定义的方法MongoCursor :: toArray()"
这是我的代码:
$getting_started_collection = $this->getServiceLocator()->get('Common\Collection\ResourcesGettingStarted');
$criteria = array(
'$or' => array(
array('affiliate_type' => 'cpl_cpm'),
array('affiliate_type' => 'cpl')
)
);
$columns = array(
'_id' => true,
'title' => true,
'description' => true,
'logo' => true,
'pdf' => true
);
$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = $cursor->toArray();
echo("<pre>");
print_r($data_array);
die();
我如何使用https://docs.mongodb.com/manual/reference/method/cursor.toArray/?
答案 0 :(得分:2)
那是因为MongoCursor
类没有名为toArray
的方法。以下是所有可用方法的列表 - MongoCursor
您应该在iterator_to_array()
手册中使用Example #1
:
<?php
$cursor = $collection->find();
var_dump(iterator_to_array($cursor));
?>
来源:http://php.net/manual/en/class.mongocursor.php
在你的例子中:
$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = iterator_to_array($cursor);
echo("<pre>");
print_r($data_array);
die();