Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

push modifier does not support embeded documents well #497

Open
foxban opened this Issue · 0 comments

1 participant

@foxban

For example, I got following document definitions:

class FileInfo
  include MongoMapper::Document
  key :filename, String
  many :file_details
  timestamps!
end

class FileDetail
  include MongoMapper::EmbeddedDocument
  key :hostname, String
  key :size, Integer
  key :mtime, Integer
  key :owner, String
  key :mode, Integer
end

create operation works as expected:

FileInfo.create( 
                :filename => "test1", 
                :_id => finfo_id, 
                :file_details => [
                  FileDetail.new( :hostname => "d01.dl.zwt", :size => 12334, :mtime => 1365544, :owner => "360", :mode => 644 ),
                  FileDetail.new( :hostname => "d02.dl.zwt", :size => 12334, :mtime => 1365544, :owner => "360", :mode => 644 ),
                  FileDetail.new( :hostname => "d03.dl.zwt", :size => 12334, :mtime => 1365544, :owner => "360", :mode => 644 ),
                  ]
               )

but when I want to push one new file_detail into existing file_info, I got two problems

Problem1:

file_info = FileInfo.find(finfo_id)
file_info.push( {:file_details => FileDetail.new( :hostname => "d04.dl.zwt", :size => 12334, :mtime => 1365544, :owner => "360", :mode => 647 )})

because finfo_id is a Sting but not a BSON::ObjectID, mongo_mapper did not make up a valid citeria to do push, and data is not updated.

Problem2:

FileInfo.push( {:_id => finfo_id}, {:file_details => FileDetail.new( :hostname => "d04.dl.zwt", :size => 12334, :mtime => 1365544, :owner => "360", :mode => 647 )})

I got the following exception:

/Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:638:in `bson_type': Cannot serialize FileDetail as a BSON type; it either isn't supported or won't translate to BSON. (BSON::InvalidDocument)
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:170:in `serialize_key_value'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:142:in `block in serialize'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:142:in `each'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:142:in `serialize'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:485:in `serialize_object_element'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:177:in `serialize_key_value'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:137:in `block in serialize'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:137:in `each'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:137:in `serialize'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/bson-1.8.2/lib/bson/bson_ruby.rb:115:in `serialize'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/mongo-1.8.2/lib/mongo/collection.rb:478:in `update'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/modifiers.rb:77:in `modifier_update'
    from /Users/wangli/.rvm/gems/ruby-1.9.3-p194/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/modifiers.rb:43:in `push'
    from test_embed.rb:63:in `<main>'

I guess to_mongo is not called before doing update, so change my code to this get around it:

FileInfo.push( {:_id => finfo_id}, {:file_details => FileDetail.new( :hostname => "d04.dl.zwt", :size => 12334, :mtime => 1365544, :owner => "360", :mode => 647 ).to_mongo})

Are the two problems bugs, or I used it in a wrong way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.