Instead of running while() or foreach() over a Mongo cursor, get all results from cursor into array:
$curs = $Mdb->tbl->find();
$arrOut = iterator_to_array( $curs, false ); // false uses numeric index.
return $arrOut;
要获得集合中的所有文档,我们需要 MongoCollection::find() 方法。 find() 方法返回一个 MongoCursor 对象,允许我们遍历整个结果集合来读取文档。要查询所有的文档并显示它们,使用:
<?php
$connection = new MongoClient();
$collection = $connection->database->collectionName;
$cursor = $collection->find();
foreach ( $cursor as $id => $value )
{
echo "$id: ";
var_dump( $value );
}
?>
$id
是集合的 _id
字段(被转换成字符串),
$value
是文档本身。
MongoCollection::find() 方法的文档中有关于查询的详细信息。
Instead of running while() or foreach() over a Mongo cursor, get all results from cursor into array:
$curs = $Mdb->tbl->find();
$arrOut = iterator_to_array( $curs, false ); // false uses numeric index.
return $arrOut;