Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB ODM SELECT COUNT(*) equivalent

I wonder if there is an equivalent to the MySQL-Query:

   SELECT COUNT(*) FROM users

in MongoDB ODM?

This might work:

$qb = $this->dm->createQueryBuilder('Documents\Functional\Users');
$qb->select('id');   
$query   = $qb->getQuery();
$results = $query->execute();
echo $query->count(); 

But aren't then all IDs returned and how does this affect performance if there are more complex documents in database. I don't want too send to much data around just to get a count.

like image 563
chris Avatar asked Jun 11 '12 10:06

chris


People also ask

How do I count the number of rows in MongoDB?

count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.

How do I count the number of documents in MongoDB?

n = count( conn , collection ) returns the total number of documents in a collection by using the MongoDB® C++ interface connection. n = count( conn , collection ,Query= mongoquery ) returns the total number of documents in an executed MongoDB query on a collection.

Where is count in MongoDB Atlas?

The Atlas Search count option adds a field to the metadata results document that displays a count of the search results for the query. You can use count to determine the size of the result set. You can use it in the $search or $searchMeta stage.


3 Answers

A small contribution:

if you run the count this way:

$count = $this->dm->createQueryBuilder('Documents\Functional\Users')
         ->getQuery()->execute()->count();

Doctrine runs this query:

db.collection.find();

however, if the code is as follows:

$count = $this->dm->createQueryBuilder('Documents\Functional\Users')
         ->count()->getQuery()->execute();

Doctrine in this case run this query:

db.collection.count();

I do not know if there is improvement in performance, but I think most optimal

I hope that is helpful

like image 68
Diego Vidal Avatar answered Nov 12 '22 20:11

Diego Vidal


$count = $this->dm->createQueryBuilder('Documents\Functional\Users')
             ->getQuery()->execute()->count();

The above will give you the number of documents inside a collection of Users. The query in question doesn't return all of the documents and then count them. It generates a cursor to the collection and from there it knows the count. Only once you start to iterate over the cursor does the driver start pulling data from the database.

A handy operator for performance is the eagerCursor(true) which will retrieve all the data in the query before hydration and close the cursor. Use this if you know the data you want to get and you'll be finished with it after the query.

Eager Cursor

If you have references that you know you will be iterating over. Use the prime(true) method on them.

Prime

If you want to return all the elements raw data, you can use hydrate(false) method in the query to disable the hydration system.

like image 36
Jamie Sutherland Avatar answered Nov 12 '22 20:11

Jamie Sutherland


For Doctrine ODM 2 you can switch query type to count before call getQuery:

    return $this->createQueryBuilder()
        ->field('storage')->equals($storage)
        ->field('priority')->in($priorities)
        ->count()
        ->getQuery()
        ->execute();
like image 37
Hett Avatar answered Nov 12 '22 18:11

Hett