Is Cursor lazy loaded in php extension when using find?

In php extension when I am using “find” method, Cursor is returned. Does execution of “find” runs a query against Mongo ? Or is it run only when Cursor is being iterated ?

I tested “find” execution with simple and complex queries just running “find” and not iterating over the cursor. I see the time is rising for complex query so that would implicate that the query is executed even when Cursor is not iterated.

Seems counter intuitive so I am asking for explanation please.

Hi @Pawel_Baranski

Welcome to MongoDB community!

PHPLIB’s Collection::find() method utilizes executeQuery() from PHPC and will execute a server command immediately. The returned Cursor object is constructed after the driver receives a response from the server.

Thanks for the response.

To make it perfectly clear - all the data that are needed to return results, response from the query that is passed to “find” is already loaded in the cursor right after calling “find” - is that correct ?

find operation that return multiple documents do not immediately return all values matching the query. Because a query can potentially match very large sets of documents, these operations return an object called a cursor, which references documents identified by the query (find operation result in simple terms). As we iterate over the cursor, the cursor fetches documents in batches from the server, to reduce both memory consumption and network bandwidth usage.

1 Like

thank you, now I understand it

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.