Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3362

Cannot access a document field named "metadata"

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 7.0.0, 12_01_17, 6.3.0
    • Affects Version/s: None
    • Component/s: None
    • None

      I have an existing Mongo database with files stored in GridFS, each with a "metadata" attribute. For example, from mongo shell:

      { "_id" : ObjectId("519380771d542c0c90676143"), "filename" : "13/39/0af8ce90-c7ff-4a8a-83c2-ec58137df8d8/Photo-1793648387.jpg", "length" : NumberLong(324509), "chunkSize" : 262144, "uploadDate" : ISODate("2013-05-15T12:32:55.939Z"), "md5" : "2cb36610feb3721ac656e0c15a1928bc", "metadata" : { "session" : "0af8ce90-c7ff-4a8a-83c2-ec58137df8d8", "project" : 39, "filename" : "Photo-1793648387.jpg" } }
      

      We keep the "session" and "project" identifiers in the metadata so that we can query files by those foreign keys. This data is stored in the "metadata" field, as is consistent with Mongo docs: http://docs.mongodb.org/manual/reference/gridfs/#gridfs-files-collection

      I'm trying to access this field using Mongoid GridFS and had no luck. Even with a basic class defined like so, I get this:

      class File
        include Mongoid::Document
        store_in collection:"fs.files"
      
        field(:filename, :type => String)
        field(:contentType, :type => String)
      
        field(:length, :type => Integer)
        field(:chunkSize, :type => Integer)
        field(:uploadDate, :type => Time)
        field(:md5, :type => String)
        field(:file_metadata, :type => Hash, :as => :metadata)
      end
      
      f = File.find("519380771d542c0c90676143")
      #=> #<File _id: 519380771d542c0c90676143, filename: "13/39/0af8ce90-c7ff-4a8a-83c2-ec58137df8d8/Photo-1793648387.jpg", contentType: nil, length: 324509, chunkSize: 262144, uploadDate: 2013-05-15 12:32:55 UTC, md5: "2cb36610feb3721ac656e0c15a1928bc", file_metadata(metadata): nil> 
      

      I was hoping that defining a field :as => :metadata would have let me access it, but no luck. Is there way to do this already, or will this require some custom patching in my File class?

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            mattconnolly mattconnolly
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: