If you’re running this code in Lithium, it’s not doing what you think it’s doing:
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:
or in MongoDB at the command line:
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: