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 wrote a book titled Professional Web APIs with PHP back in 2006, and am currently working in Biomedical Informatics for a major public health company.

I’m working on a project to help developers called WonderProxy which has proxies all over the world. Working on GeoIP development? Now you can finally test properly! We've also released Global Ping Statistics for expected ping times between cities, as well as a Load Testing Tool to measure your site's ability to handle load. Our most recent site checking tool is Where's it Up? which checks your sites availability globally, returning HTTP, DNS, and Traceroute details

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

I co-founded:

WonderNetwork Logo