Mongo-PHP - MongoCursor异常与MongoDB PHP驱动程序v1.6 [英] Mongo-PHP - MongoCursor exception with MongoDB PHP Driver v1.6

查看:632
本文介绍了Mongo-PHP - MongoCursor异常与MongoDB PHP驱动程序v1.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从PHP MongoCursor遇到麻烦从我升级到Mongo PHP驱动程序从1.5.8到1.6.0

I'm having troubles with PHP MongoCursor since I upgraded to Mongo PHP Driver from 1.5.8 to 1.6.0

以下代码适用于版本1.5。 8,但是与1.6版本崩溃

The following code works well with version 1.5.8, but crashes with version 1.6

PHP版本是5.5.21,Apache版本是Apache / 2.4.10(Ubuntu)

PHP version is 5.5.21., Apache version is 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()调用会与此消息一起崩溃:

And so the hasNext() call crashes with this message :


CRITICAL:MongoException:MongoCursor对象未被其构造函数(未捕获异常)正确初始化...

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

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

Am I doing something wrong ? Thanks for you help !

推荐答案

这可能与1.6.0中介绍的有关迭代的错误有关 hasNext() getNext() PHP-1382 。此后,修复程序已合并到 v1.6 分支,应该在本周稍后发布为1.6.1。

This may be related to a bug that was introduced in 1.6.0 regarding iteration with hasNext() and getNext(): PHP-1382. A fix has since been merged to the v1.6 branch and should be released later this week as 1.6.1.

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

That said, the bug regarding hasNext() was actually that the last document in the result set would be missed while iterating. If I run your original script against 1.6.0, the array contains a null value as its last element. With the fix in place, the array will contain all documents as is expected. I cannot reproduce the exception you're seeing with either version.

该异常实际上是从C数据结构的内部检查中抛出的,以确保游标对象正确关联一个MongoClient和套接字连接。在 MONGO_CHECK_INITIALIZED()宏调用L234rel =nofollow>此文件。大多数游标方法检查一个MongoClient是否关联,但 hasNext()是唯一的,因为它还检查套接字对象(我相信其他方法只是假设一个游标用MongoClient也有一个socket)。如果这个异常是真正可重现的你,你愿意做一些调试与扩展,我会很感兴趣,知道这两个检查中的哪一个是抛出的错误。

That exception is actually thrown from an internal checks on the C data structures, to ensure that the cursor object is properly associated with a MongoClient and socket connection. See the MONGO_CHECK_INITIALIZED() macro calls in this file. Most all of the cursor methods check that a MongoClient is associated, but hasNext() is unique in that it also checks for the socket object (I believe other methods just assume a cursor with a MongoClient also has a socket). If that exception is truly reproducible for you and you're willing to do some debugging with the extension, I'd be very interested to know which of the two checks is throwing the error.

另外,在构建MongoClient时,还应该指定replicaSet选项。这应该具有副本集名称,以确保驱动程序可以正确地忽略与不是预期副本集的成员的主机的连接。

As a side note, you should also be specifying the "replicaSet" option when constructing MongoClient. This should have the replica set name, which ensures that the driver can properly ignore connections to hosts that are not a member of the intended replica set.

这篇关于Mongo-PHP - MongoCursor异常与MongoDB PHP驱动程序v1.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆