If you’re running this code in Lithium, it’s not doing what you think it’s doing:

$count = Results::find(’all’, array(’conditions’ => array(’fruit’ => ‘apples’)))->count();

This is a really easy line of code to end up writing, especially if you’ve been reading the MongoDB docs a bunch, or are just porting some code into the application. That’s because if you were accessing the MongoDB driver directly you would want:

$count = $collection->find(array(’fruit’ => ‘apples’)->count();

or in MongoDB at the command line:

db.collection.find({’fruit’ : ‘apples’}).count();

They all seem very similar, but the first one is actually returning all the results from MongoDB to PHP, lithium is then obligingly counting the results, returning that count, and then throwing away all the data. If your data set is tiny, the difference here will be negligible. However with large result sets, or simply results that contain large documents, the difference could be catastrophic.

What you’re looking for is:

$count = Results::find(’count’, array(’conditions’ => array(’fruit’ => ‘apples’)));


Comments »

No Trackbacks
No comments

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
 

Hi, I’m Paul Reinheimer, a developer working on the web.

I co-founded WonderProxy which provides access to over 200 proxies around the world to enable testing of geoip sensitive applications. We've since expanded to offer more granular tooling through Where's it Up

My hobbies are cycling, photography, travel, and engaging Allison Moore in intelligent discourse. I frequently write about PHP and other related technologies.

Search