简体   繁体   English

原则MongoDB捆绑:当“ _id”是数组时,游标toArray()错误

[英]Doctrine MongoDB Bundle: Cursor toArray() Error When “_id” is an Array

Resulting from a MapReduce, I have a MongoDb collection that has the following structure: 由MapReduce产生的结果是,我有一个具有以下结构的MongoDb集合:

{ "_id" : { "id" : NumberLong(1), "date" : "04-26-2012" }, "value" : { "count" : 100 } }

In my controller I am doing the following to return an array to display the results: 在我的控制器中,我正在执行以下操作以返回一个数组以显示结果:

$mongoDb         = $mongo->selectDatabase($dbname);        
$mongoCollection = $mongoDb->selectCollection($collname);
$qb              = $mongoCollection->createQueryBuilder(); 
$qb              = $qb->find();        
$resultCursor    = $qb->getQuery()->execute();
                                  ->limit(10);
$resultArray     = $resultCursor  ->toArray();

However, I get an exception: "Notice: Array to string conversion in vendor/doctrine-mongodb/lib/Doctrine/MongoDB/Cursor.php line 154" 但是,我得到一个例外:“注意:在vendor / doctrine-mongodb / lib / Doctrine / MongoDB / Cursor.php第154行中数组到字符串的转换”

Below is line 154 of Cursor.php. 下面是Cursor.php的154行。 Does MongoCursor::key not handle "_id" as an Array? MongoCursor :: key是否不能将“ _id”作为数组处理?

/** @proxy */
public function key()
{
    return $this->mongoCursor->key();
}

For fast converting mongoDb cursor to array you may use http://php.net/manual/en/function.iterator-to-array.php 为了将mongoDb光标快速转换为数组,您可以使用http://php.net/manual/en/function.iterator-to-array.php

$qb = $this->createQueryBuilder();

$qb->hydrate(false);

$query = $qb->getQuery();

$resultArray = iterator_to_array($query->execute());

key() always returns a string (see http://php.net/manual/en/class.iterator.php ), so it's generating that notice trying to convert an array into string form. key()总是返回一个字符串(请参阅http://php.net/manual/zh/class.iterator.php ),因此它会生成尝试将数组转换为字符串形式的通知。 It is only a notice, though, it should still work. 不过,这只是一个通知,它仍然应该起作用。

The easiest way around this is probably just not to call toArray() on the cursor: iterate through it instead ( foreach $resultCursor as $value) ... ). 解决此问题的最简单方法可能是不对游标调用toArray():而是对其进行迭代( foreach $resultCursor as $value) ... )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM