There is a way to solve this:
@Document(collection="fs.files")
public class MyGridFsFile {
    @Id
    private ObjectId id;
    public ObjectId getId() { return id; }
    private String filename;
    public String getFilename() { return filename; }
    private long length;
    public long getLength() { return length; }
    ...
}
You can write a normal Spring Mongo Repo for that. Now you can at least query the fs.files collection using a Spring Data Repo. But: You cannot access the file contents this way.
For getting the file contents itself, you've got (at least) 2 options:
- Use - file = gridOperations.findOne(Query.query(Criteria.where("_id").is(id)));- InputStream is = file.getInputStream();
 
- Have a look at the source code of - GridFSDBFile. There you can see, how it internally queries the- fs.chunkscollection and fills the InputStream.
 
(Option 2 is really low level, Option 1 is a lot easier and this code gets maintained by the MongoDB-Java-Driver devs, though Option 1 would be my choice).
Updating GridFS entries:
- GridFS is not designed to update file content!
- Though only updating the metadatafield can be useful. The rest of the fields is kinda static.
You should be able to simply use your custom MyGridFsFileRepo's update method. I suggest to only create a setter for the metadata field.
Different metadata for different files:
I solved this using an abstract MyGridFsFile class with generic metadata, i.e.:
@Document(collection="fs.files")
public abstract class AbstractMyGridFsFile<M extends AbstractMetadata> {
    ...
    private M metadata;
    public M getMetadata() { return metadata; }
    void setMetadata(M metadata) { this.metadata = metadata; }
}
And of course each impl has its own AbstractMetadata impl associated. What have I done? AbstractMetadata always has a field called type. This way I can find the right AbstractMyGridFsFile impl. Though I have also a generic abstract repository.
Btw: In the meantime I switched here from using Spring Repo, to use plain access via MongoTemplate, like:
protected List<A> findAll(Collection<ObjectId> ids) {
    List<A> files = mongoTemplate.find(Query.query(Criteria
            .where("_id").in(ids)
            .and("metadata.type").is(type) // this is hardcoded for each repo impl
    ), typeClass); // this is the corresponding impl of AbstractMyGridFsFile
    return files;
}
Hope this helps. I can write more, if you need more information about this. Just tell me.