Search code examples
mongodbspring-bootmongodb-querygridfsgridfs-stream

Creating query to get some certain parts from a file GRIDFS


In my Spring Boot application I used GridFS to store large file in my database. To find certain files, I use normal queries on the files collection:

GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where(ID).is(id)));

but with this approach I'm getting the entire file.

My question is, how to create some queries without loading the whole file in the memory? My stored files are books ( in pdf format ) and suppose I want to get the content from certain page without loading the entire book in the memory. I'm guessing I'll have to use the chunk collection and perform some operations to the chunks but I cannot find how to do that.


Solution

  • GridFS is described here. Drivers do not provide a standardized API for retrieving parts of the file, but you can read that spec and construct your own queries that would retrieve portions of the written chunks.

    Your particular driver may provide partial file retrieval functionality, consult its docs for that.