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

Updating an embedded document triggers an unnecessary unset

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0
    • Affects Version/s: 5.2.0
    • Component/s: None
    • None

      When updating an embedded document in a 1-1 relation an unnecessary unset is triggered prior to the set. This unset is a redundant operation that is both wasting resources and breaking the atomicity of the update. The same problem is discussed here. The following RSpec test demonstrates the issue:

      describe 'embedded 1-1 relations' do
        class Order
          include Mongoid::Document
      
          embeds_one :address
        end
      
        class Address
          include Mongoid::Document
      
          embedded_in :order
      
          field :city, type: String
        end
      
        let(:address) { Address.new(city: 'London') }
        let(:order) { Order.create!(address: address) }
      
        it 'do not call unnecessary unsets' do
          Rails.logger.debug('------- start -------')
          # The below statement results in two updates, the first is unnecessary
          # {"update"=>"orders", "updates"=>[{"q"=>{"_id"=>BSON::ObjectId('58da22a2e9c7a20dba19bf83')}, "u"=>{"$unset"=>{"address"=>true}}, "multi"=>false, "upsert"=>false}], "ordered"=>true}
          # {"update"=>"orders", "updates"=>[{"q"=>{"_id"=>BSON::ObjectId('58da22a2e9c7a20dba19bf83')}, "u"=>{"$set"=>{"address"=>{"_id"=>BSON::ObjectId('58da22a2e9c7a20dba19bf85'), "city"=>"Berlin"}}}, "multi"=>false, "upsert"=>false}], "ordered"=>true}
          order.address = Address.new(city: 'Berlin')
          Rails.logger.debug('------- end -------')
        end
      end
      

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

              Created:
              Updated:
              Resolved: