doctrine / mongodb

PHP MongoDB Abstraction Layer
https://www.doctrine-project.org/projects/mongodb.html
MIT License
442 stars 190 forks source link

Use primary read preference when reading newly peristed GridFS files #324

Closed alcaeus closed 6 years ago

alcaeus commented 6 years ago

This bug can appear when working on a replica set and assigning GridFS documents a secondary read preference (e.g. via the @ReadPreference mapping in Doctrine MongoDB ODM). The library stores the file, then reads it from the database to fetch current metadata. If the document is using a secondary read preference, this can lead to the document not being found. Furthermore, due to insufficient error handling, this will not be a regular exception but a type error when assigning the result to the GridFSFile instance.

Last but not least, the file was read twice from the database with the result of the first read not being used, so this should also (slightly) improve performance of GridFS write operations.