Resulting from a MapReduce, I have a MongoDb collection that has the following structure:
{ "_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”
Below is line 154 of Cursor.php. Does MongoCursor::key not handle “_id” as an Array?
/** @proxy */
public function key()
{
return $this->mongoCursor->key();
}
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. 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) ...).