Question

Empirically it seems that flush() is not necessary after findAndUpdate(), I just couldn't find this explicitly stated anywhere in the Doctrine ODM/MongoDB docs (and I didn't bother to read much source code).

The findAndModify docs on mongodb.org state

This command can be used to atomically modify a document (at most one) and return it.

And Doctrine MongoDB's findAndUpdate() uses MongoDB's findAndModify. So it sounds like the whole thing does indeed happen in one go, so calling flush() on the document manager shouldn't be necessary.

Was it helpful?

Solution

Flush is only needed for writing changes to managed objects back to Mongo. Anything you do through the query builder interface will be executed directly and bypass UnitOfWork. This is especially true for updates and upserts. In the case of findAndUpdate(), the update should be executed in Mongo immediately, but I believe the object returned might be managed. Any changes to that document afterwards (e.g. via setter methods) would need a flush() if you wanted them written back to Mongo.

Also, be aware of returnNew() on the query builder, which corresponds to the new option of findAndModify. By default, I believe findAndUpdate() will return the document in its pre-updated state. You may prefer to retrieve the document in its updated state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top