Question

I'm considering using multiple gridfs kind of like folders. The idea being that then deletion of folders is easy. Just delete the file system. I've just one problem. I don't see any way of deleting them? If deleting them means I have to loop through all of them and delete them one by one then doing this makes no sense.. So if someone more knowledgeable with gridfs can confirm if this can be done and is it a stupid idea? Also it might be extremely unwise to create a separate filesystem with its own indexes etc. of each "file/folder" and thus possibly resulting in thousands of separate filesystems being created... (However, don't just say it's a stupid idea, it is still a valid question whether or not it can be done and if so, how, even if it would be unwise in my particular use case.)

Here's an illustration of what I'm trying to achieve with the ruby driver:

file = "some_image.jpg"
grid = Mongo::Grid.new(Mongoid.database, File.join(cache_dir, File.basename(file))
genereate_versions_of(file).each do |version| #e.g. micro.jpg, thumbnail.jpg, large.jpg
  grid.put version
end

#and then later when all of the versions of this file needs to be deleted I'd like
#to do something like this... but it seems it's not possible?
Mongo::Grid.destroy(Mongoid.database, File.join(cache_dir, File.basename(file))
#or
grid = Mongo::Grid.new(Mongoid.database, File.join(cache_dir, File.basename(file))
grid.destroy
Was it helpful?

Solution

GridFS is just an abstraction layer on top of normal collections and are represented by two collections: .files and .chunks. You can just delete those two collections to "delete your GridFS". The should be the same as File.join(cache_dir, File.basename(file)).

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