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

mongoid generating inconsistent atomic operations

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.7
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I have observed two scenarios where mongoid 2.2.0 generates inconsistent atomic operations. These used to work fine in 2.0.2. I observed both these using nested attributes, so am not sure how to recreate without using them.

      • When documents to embeds_many are added and removed in a single operation, mongoid sends $pushAll and $pullAll in a single update statement to mongodb, which results in a

      > Mongo::OperationFailure: 10150: Field name duplication not allowed with modifiers

      Rcreation:

      class Foo
        include Mongoid::Document
        field :name
        embeds_many :bars
        accepts_nested_attributes_for :bars, :allow_destroy => true
      end
      
      class Bar
        include Mongoid::Document
        field :name
        embedded_in :foo
      end
      
      foo = Foo.create(:name => "foo1", :bars => [{:name => "bar1"}, {:name => "bar2"}])
      foo.reload
      
      params = {
        "foo" => {
          "bars_attributes" => {
            "0" => {"name" => foo.bars[0].name, "id" => foo.bars[0].id, "_destroy" => "false"},
            "1" => {"name" => foo.bars[1].name, "id" => foo.bars[1].id, "_destroy" => "1"},
            "12341" => {"name" => "bar3", "_destroy" => "false"},
          }
        }
      }
      
      foo.safely.update_attributes(params["foo"])
      # Mongo::OperationFailure: 10150: Field name duplication not allowed with modifiers
      # from /home/rubish/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:451:in `send_message_with_safe_check'
      
      # operation sent to mongodb if not using safely
      # MONGODB alma_connect['foos'].update({"_id"=>BSON::ObjectId('4e653346932ecf65a1000009')}, {"$pullAll"=>{"bars"=>[{"name"=>"bar2", "_id"=>BSON::ObjectId('4e653346932ecf65a100000b')}]}, "$pushAll"=>{"bars"=>[{"name"=>"bar3", "_id"=>BSON::ObjectId('4e653346932ecf65a100000c')}]}})
      
      • When there is nested document of nesting level at least two, i.e. Foo embeds_many Bars embeds_one Baz, mongoid tries to push baz in bars array as well as set it independently, resulting in

      > Mongo::OperationFailure: 10151: have conflicting mods in update

      Recreation:

      class Foo
        include Mongoid::Document  
        embeds_many :bars
        accepts_nested_attributes_for :bars, :allow_destroy => true
        field :name
      end
      
      class Bar
        include Mongoid::Document
        embedded_in :foo
        embeds_one :baz
        accepts_nested_attributes_for :baz, :allow_destroy => true
        field :name
      end
      
      class Baz
        include Mongoid::Document
        embedded_in :bar
        field :name
      end
      
      foo = Foo.create(:name => "foo1")
      
      params = {
        "foo" => {
          "bars_attributes" => {
             "0" => {
               "name" => "bar1",
               "baz_attributes" => {"name" => "baz1"}
             }
          } 
        }
      }
      
      foo.safely.update_attributes(params["foo"])
      # Mongo::OperationFailure: 10151: have conflicting mods in update
      # from /home/rubish/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:451:in `send_message_with_safe_check'
      
      # operation sent to mongodb if not using safely
      # MONGODB alma_connect['foos'].update({"_id"=>BSON::ObjectId('4e6532c2932ecf6591000009')}, {"$pushAll"=>{"bars"=>[{"name"=>"bar1", "_id"=>BSON::ObjectId('4e6532c4932ecf659100000a'), "baz"=>{"name"=>"baz1", "_id"=>BSON::ObjectId('4e6532c4932ecf659100000b')}}]}, "$set"=>{"bars.baz"=>{"name"=>"baz1", "_id"=>BSON::ObjectId('4e6532c4932ecf659100000b')}}})
      

      I am using rails 3.0.10, mongoid 2.2.0 and mongodb 1.8.3

            Assignee:
            durran Durran Jordan
            Reporter:
            rubish rubish
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: