Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the PHP MongoDB Driver's Cursor buffer a result set?

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?

like image 476
Jim Rubenstein Avatar asked May 05 '11 14:05

Jim Rubenstein


1 Answers

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.

like image 189
Gates VP Avatar answered Sep 23 '22 17:09

Gates VP