Doctrine call MongoCursor::rewind when I try get singleResult from Query
vsychov opened this issue · 1 comments
Hi, I found some strange issue, when I try to get single record from DB, by code:
$this
->createQueryBuilder()
->field('field1')->equals('value1')
->getQuery()
->getSingleResult();
then Cursor::toArray, convert cursor to array by iterator_to_array, and iterator_to_array call MongoCursor::rewind (it's not good for perfomance).
If I call findOneBy, MongoCursor::rewind not called.
Do you have evidence that the query is actually being executed multiple times? Or is the concern just that we're invoking a couple of extra cursor methods (through iterator_to_array()
's execution)?
Cursor::getSingleResult()
does reset the cursor before and after executing it, but rewind()
should only be called once. The iterator_to_array()
behavior should be no different than iterating with foreach
and breaking after the first iteration, since we're limiting the reset set to one document.
At the driver level, MongoCursor::findOne()
is effectively doing the same thing as MongoCursor::rewind()
, followed by valid()
and current()
. iterator_to_array()
effectively calls those methods for us.
Doctrine MongoDB's query builder works against a cursor, which allows it work nicely with the cursor provided for ODM (when hydration is required). For that reason, we don't have a code path that shortcuts to MongoCollection::findOne()
.