mongoid-grid_fs icon indicating copy to clipboard operation
mongoid-grid_fs copied to clipboard

How to specify a separate gridfs host?

Open raulccabreu opened this issue 8 years ago • 1 comments

Instead of connect to the mongoid configured one (usually the used to store model documents).

This can be useful on cluster configurations with different number for files and documents hosts.

raulccabreu avatar Apr 16 '17 14:04 raulccabreu

I had to figure this out for myself as well, since the code is a bit less than clear on how to do this, but it is possible, by giving the File and Chunk model a store_in hash.

gridfs = Mongoid::GridFs
gridfs.file_model.store_in database: 'db_name_here'
gridfs.chunk_model.store_in database: 'db_name_here'

This can then be confirmed with:

gridfs.file_model.collection

In my apps file model (in this case, for ActiveDirectory user photos), I have a convenience memoized set up method:

def self.gridfs
  @gridfs ||= begin
    gridfs = Mongoid::GridFs

    gridfs.file_model.store_in database: AD.database_name
    gridfs.chunk_model.store_in database: AD.database_name

    gridfs
  end
end

So far, that seems to work, allowing me to store those files in a single database, but access them from 3 separate web-apps.

syntruth avatar Apr 03 '18 17:04 syntruth