Skip to content

How to specify a separate gridfs host? #68

Closed

Description

@raulccabreu

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.

Activity

  1. syntruth commented on Apr 3, 2018

    @syntruth

    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.

  2. dblock commented on Oct 18, 2026

    @dblock
    Contributor

    Configure a named Mongoid client with the separate hosts/database, then select it on BOTH GridFS models during application initialization:

    gridfs = Mongoid::GridFs.namespace_for(:attachments)
    gridfs.file_model.store_in client: :gridfs
    gridfs.chunk_model.store_in client: :gridfs

    For database-only changes, use store_in database: "files_database" on both models. Mongoid 3/4 use named sessions (store_in session: :gridfs). This is shared model configuration, not a per-request switch. #89 documents the existing configuration API.

    Posted by an AI on behalf of @dblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions