php - Mongo-PHP - MongoDB PHP 驱动程序 v1.6 的 MongoCursor 异常

标签 php mongodb cursor

自从我将 Mongo PHP 驱动程序从 1.5.8 升级到 1.6.0 后,我在使用 PHP MongoCursor 时遇到了问题

以下代码在 1.5.8 版本中运行良好,但在 1.6 版本中崩溃

PHP 版本为 5.5.21.,Apache 版本为 Apache/2.4.10 (Ubuntu)

$mongoClient = new \MongoClient($serverUrl, ['readPreference'=>\MongoClient::RP_NEAREST]);
$database = $mongoClient->selectDB($dbName);
$collection = $database->selectCollection($collectionName);

// count() works fine and returns the right nb on documents
echo '<br/>count returned '.$collection->count();

// find() exectues with no error...
$cursor = $collection->find();
$documents = [];
// ...and hasNext() crashes with the Excetion below
while($cursor->hasNext()){$documents[] = $cursor->getNext();}
return $documents;

因此 hasNext() 调用崩溃并显示以下消息:

CRITICAL: MongoException: The MongoCursor object has not been correctly initialized by its constructor (uncaught exception)...

我做错了什么吗? 感谢您的帮助!

最佳答案

这可能与 1.6.0 中引入的有关 hasNext()getNext() 迭代的错误有关:PHP-1382 。修复已合并到 v1.6 branch将于本周晚些时候作为 1.6.1 发布。

也就是说,有关 hasNext() 的错误实际上是在迭代时会丢失结果集中的最后一个文档。如果我针对 1.6.0 运行原始脚本,则该数组包含一个 null 值作为其最后一个元素。修复到位后,该数组将按预期包含所有文档。我无法重现您在任一版本中看到的异常。

该异常实际上是由对 C 数据结构的内部检查引发的,以确保游标对象与 MongoClient 和套接字连接正确关联。请参阅 this file 中的 MONGO_CHECK_INITIALIZED() 宏调用。大多数游标方法都会检查 MongoClient 是否关联,但 hasNext() 的独特之处在于它还检查套接字对象(我相信其他方法只是假设带有 MongoClient 的游标也有一个 socket )。如果您确实可以重现该异常,并且您愿意使用扩展进行一些调试,那么我很想知道这两个检查中的哪一个抛出了错误。


作为旁注,您还应该在构造 MongoClient 时指定 "replicaSet" 选项。这应该具有副本集名称,这确保驱动程序可以正确忽略与不属于预期副本集成员的主机的连接。

关于php - Mongo-PHP - MongoDB PHP 驱动程序 v1.6 的 MongoCursor 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28275916/

相关文章:

php - 在另一台服务器上获取脚本输出的最佳方法是什么?

node.js - 如果键不存在,则将值推送到数组 Mongoose

C++检查光标位置

php - 如何从数据库中获取数据并在 PHP 中显示?

php - Symfony 1.4 导入资源 yml

php - 使用 jQuery 将 JSON 发布到 Laravel 4

node.js - 连接到 MongoLab 数据库时验证失败,代码 18

javascript - MongoDB : How can I reformat this code to include a sort on the name field?

java - 更改光标以移交 JFrame 中的某些坐标

postgresql - Psycopg2 DictCursor 什么都不返回