$result = $collection->find('some constraints')->limit(10);
Now suppose the documents satisfying "some constraints" are 10000.
So, does this query first fetch 10000 documents in memory and returns 10 to me or does it load only 10 documents specified by limit
?
I've recently written an article about this. In short, when you use limit, the driver will request only 10 items from the server so on the client side, not more than 10 documents will be in memory. "limit" is strictly a client thing, and is only used while fetching the documents. The query will still run in full.
On the server side, all documents that match your find() constraint are going to be read which means the OS will put those into memory from disk, if they are not in memory yet. But then again, from the the MongoDB server process, all documents are always in memory anyways; it's up the operating system to do the caching of this, and paging to and from disk. Read more about how MongoDB manages memory at their docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With