john Smith
john Smith

Reputation: 17906

Doctrine2 ODM Limit doesn't work / mongodb

I'm trying to skip and limit results, but I don't even get it to limit the results.

here's my code

    $limit=5;
    $fooQueryBuilder = $this->mongo->getManager()->createQueryBuilder('CustomCoreBundle:Foo');
    $foos=$fooQueryBuilder->limit($limit)->getQuery()->execute();

    var_dump(count($foos));
    exit;

and the var_dump returns

int(321235)

and that's equal to all entities in the database, what am I doing wrong ?

$this->mongo->getManager() 

is instance of

Doctrine\ODM\MongoDB\DocumentManager

and the builder is instance of

Doctrine\ODM\MongoDB\Query\Builder"

I just don't understand what's wrong, thanks for any hint

Upvotes: 4

Views: 1503

Answers (3)

Krasimir Bosilkov
Krasimir Bosilkov

Reputation: 1

You can use $result->count($foundOnly = true)

Upvotes: 0

john Smith
john Smith

Reputation: 17906

So, I found out the answer myself

it is working !

and I learned:

when you count() a cursor, no matter what the query, it returns the amount of all entities in database.

So my check to count the cursor to see if the limit is working was simply wrong

Upvotes: 10

Joe Naber
Joe Naber

Reputation: 537

use count($result->toArray()) that solved my issue

count($result->toArray())

Upvotes: 0

Related Questions