函数:MongoDB\Driver\Cursor::rewind()
概述:这个函数用于将游标重置到结果集的开头。
适用版本:MongoDB PHP扩展版本1.2.0及以上
用法:
public function rewind(): void
参数:无
返回值:无
示例:
// 连接到MongoDB
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// 查询数据
$query = new MongoDB\Driver\Query([]);
$cursor = $manager->executeQuery("mydb.mycollection", $query);
// 遍历结果集并输出文档
foreach ($cursor as $document) {
echo $document->name . "\n";
}
// 重置游标到结果集开头
$cursor->rewind();
// 再次遍历结果集并输出文档
foreach ($cursor as $document) {
echo $document->name . "\n";
}
解释:
注意事项: