When queries are made to mongodb, how does it's cursor deal with the result set in memory? Does the cursor retrieve all documents which match the query, at once? or does it retrieve 1 document at a time? or are they buffered? or is there a different solution I don't know about?
If it's a buffered solution, how are they stored on the server/client? How much data does the client keep locally?
The MongoDB wire protocol has specifications for batch size when issuing a query.
The basic premise is that the client driver issues a query with numberToReturn
flag. If the query matches over the numberToReturn
, then only that number is returned to the client.
So the server effectively sends one "batch" to the client. If the client cycles through the whole batch, the client issues a getmore request and receives the next batch. In the meanwhile, the server does not need to load all results into memory, only enough to satisfy the client's request.
The PHP driver abstracts away much of this complexity. All you do with the driver is request the next item and the driver will handle the getmore
where appropriate.
In terms of size, you will get the smaller of Max BSON size or numberToReturn
. So if the documents are too big, you may hit Max BSON size to prevent sending too much data at once.
The best spot to get any more details is the actual code.
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