You are browsing a version that is no longer maintained.

Eager Cursors

With a typical MongoDB cursor, it stays open during iteration and fetches batches of documents as you iterate over the cursor. This isn't bad, but sometimes you want to fetch all of the data eagerly. For example when dealing with web applications, and you want to only show 50 documents from a collection you should fetch all the data in your controller first before going on to the view.

Benefits:

  • The cursor stays open for a much shorter period of time.
  • Data retrieval and hydration are consolidated operations.
  • Doctrine has the ability to retry the cursor when exceptions during interaction with mongodb are encountered.

Example:

1<?php $qb = $dm->createQueryBuilder('User') ->eagerCursor(true); $query = $qb->getQuery(); $users = $query->execute(); // returns instance of Doctrine\MongoDB\ODM\EagerCursor
2
3
4
5
6

At this point all data is loaded from the database and cursors to MongoDB have been closed but hydration of the data in to objects has not begun. Once insertion starts the data will be hydrated in to PHP objects.

Example:

1<?php foreach ($users as $user) { echo $user->getUsername()."\n"; }
2
3
4
5

Not all documents are converted to objects at once, the hydration is still done one document at a time during iteration. The only change is that all data is retrieved first.