PHP7 MongoDB Invalid reply to find command

错误信息:

PHP Fatal error: Uncaught MongoDB\\Driver\\Exception\\RuntimeException: Invalid reply to find command. in .../library/MongoDB/Operation/Find.php:299
Stack trace:
#0 .../library/MongoDB/Operation/Find.php(299): MongoDB\\Driver\\Server->executeQuery('crm.t_accountin...', Object(MongoDB\\Driver\\Query), Array)
#1 .../library/MongoDB/Operation/FindOne.php(126): MongoDB\\Operation\\Find->execute(Object(MongoDB\\Driver\\Server))
#2 .../library/MongoDB/Collection.php(657): MongoDB\\Operation\\FindOne->execute(Object(MongoDB\\Driver\\Server))
#3 .../Mongo.php(211): MongoDB\\Collection->findOne(Array, Array)
#4 xxx.php(315): xxx->fetchRow('xxx...', Array, 'xxx...')
#5 xxx.php(220): xxx->fetchR in xxx/library/MongoDB/Operation/Find.php on line 299

网上有些说这个问题在 1.2 版本中已经修复了:
PHPC-625
mongodb/mongo-php-driver#174
mongodb/mongo-php-driver#527

语法是根据 php-library findOne文档 来的

自己操作类中的 fetchRow,其中私有函数都是些简单的转换:

/**
 * 返回一行数据
 *
 * @param string $collectionName 集合名
 * @param array $condition 条件
 * @param string|null $fields 字段
 * @return array|bool
 */
public function fetchRow($collectionName, $condition = [], $fields = null) {
    $options = [];
    if (! empty($fields)) {
        $options['projection'] = $this->_toFieldsArray($fields);
    }
    $result = $this->getConnection()
        ->selectCollection($this->database, $collectionName)
        ->findOne($condition, $options);
    $result = $this->_toArray($result);
    
    return $this->_checkCharset($result, false);
}
阅读 3.6k
2 个回答

MongoDB版本 3.2.0
MongoDB extension version 1.5.1 stable

If you have reproducible errors on the most recent stable release of the driver (1.5.x), please open a new issue so we can investigate and track that going forward. I'm not sure why you would be calling pcntl_fork() from a web SAPI, but note that the driver still does not support sharing its resources across forked PHP processes. The fixes I referred to in #174 (comment) only pertain to including the PID when persisting libmongoc client objects (and their sockets), which ensure that creating a new Manager in a child process with the same URI will not attempt to re-use resources from the parent process.


function doQuery($pid)
{
    $manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
    $collection = new MongoDB\Collection($manager, "test.test");
 
    for ($completed = false; ! $completed;) {
        try {
            $cursor = $collection->find([], []);
            $completed = true;
        } catch (MongoDB\Driver\Exception\RuntimeException $e) {
            printf("PID %d exception, retrying...\n", $pid);
        }
    }
 
    printf("PID %d completed!\n", $pid);
}
 
for ($i = 0; $i < $threads; $i++) {
    $pid = pcntl_fork();
    doQuery($pid);
}

把你写的代码贴出来

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题