ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: 9.1.1
    • Component/s: Associations
    • None
    • None
    • Ruby Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      When an empty array is assigned to an embeds_many of an embedded document with assign_attributes (or attributes=), and a non-empty array is assigned to the same association before save, the delayed $unset from the first assignment is not discarded.

      • If the second assignment uses the setter, the setter writes the new documents immediately, and the following save runs the stale $unset. No error is raised, and the association is silently removed from the database.
      • If the second assignment also uses assign_attributes, the save sends $unset and $set on the same path and fails with code 40 (ConflictingUpdateOperators).

      It does not happen when the embeds_many is defined on the root document.

      Environment

      • Mongoid 9.1.1 and master 90b76e588
      • mongo driver 2.26.0
      • Ruby 4.0.7
      • MongoDB 8.0 (replica set)

      Models

      class Leaf
        include Mongoid::Document
        field :name, type: String
        embedded_in :item
      end
      
      class Item
        include Mongoid::Document
        embeds_many :leaves, class_name: 'Leaf'
        embedded_in :tree
      end
      
      class Tree
        include Mongoid::Document
        embeds_many :items
      end
      

      Steps to reproduce

      # Case 1: assign_attributes with [] and then the setter
      id = Tree.create!(items: [Item.new(leaves: [Leaf.new(name: 'a')])]).id
      tree = Tree.find(id)
      item = tree.items.first
      item.assign_attributes(leaves: [])
      item.leaves = [Leaf.new(name: 'b')]
      tree.save!
      Tree.collection.find(_id: id).first['items'].first['leaves']
      # => nil (expected: [{ "_id" => ..., "name" => "b" }])
      
      # Case 2: assign_attributes twice
      id = Tree.create!(items: [Item.new(leaves: [Leaf.new(name: 'a')])]).id
      tree = Tree.find(id)
      item = tree.items.first
      item.assign_attributes(leaves: [])
      item.assign_attributes(leaves: [{ name: 'b' }])
      tree.save!
      # => Mongo::Error::OperationFailure: [40]: Updating the path 'items.0.leaves' would create a conflict at 'items.0.leaves'
      

      The update commands sent by the two cases (command monitoring):

      Case 1:
        {"q":{"_id":...,"items._id":...},"u":{"$set":{"items.$.leaves":[{"_id":...,"name":"b"}]}}}   # by the setter
        {"q":{"_id":...},"u":{"$unset":{"items.0.leaves":true}}}                                       # by save!
      Case 2:
        {"q":{"_id":...},"u":{"$unset":{"items.0.leaves":true},"$set":{"items.0.leaves":[{"_id":...,"name":"b"}]}}}
      

      Analysis

      The key used to add the delayed $unset differs from the key used to remove it.

      • Atomic#add_atomic_unset adds the document under the association name (document.association_name.to_s, i.e. "leaves").
      • The non-blank branch of Batchable#batch_replace removes it with _base.delayed_atomic_unsets.delete(path), where path is the absolute path ("items.0.leaves").

      On the root document both keys are "leaves", so the entry is removed. On an embedded document they differ, so the entry from the first assignment stays and is applied at save.

      The line was _base.delayed_atomic_unsets.clear until the fix of MONGOID-5352 (commit 5c1c60fc9f2bc8656e78187f50bd942773df93d8) changed it to delete(path). So this has happened since that change (8.0.0 and the 7.x releases that include the backport).

      MONGOID-5197 is the opposite case (a delayed $set left over after assigning an empty array), and it is fixed.

      Possible fix

      Also remove the entry under the association name in the non-blank branch of batch_replace:

      elsif _target != docs
        _base.delayed_atomic_sets.delete(path) unless _assigning?
        docs = normalize_docs(docs).compact
        _target.clear and _unscoped.clear
        _base.delayed_atomic_unsets.delete(path)
        _base.delayed_atomic_unsets.delete(_association.name.to_s)
        clear_atomic_path_cache
        inserts = execute_batch_set(docs)
        add_atomic_sets(inserts)
      end
      

      delete(path) is kept because remove_attribute adds its $unset under the absolute path. With this change, both cases above save the new leaf named "b" on master, and assigning only [] still unsets the association.

            Assignee:
            Unassigned
            Reporter:
            Masato Ikeda (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: