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

update_all lost data when using with $addToSet

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 9.0.0, 8.0.7, 8.1.3
    • Affects Version/s: None
    • Component/s: Persistence
    • None

      (Originally reportedĀ at https://github.com/mongodb/mongoid/discussions/5674)

      $addToSet is weird.

      Here is the model:

      class BuggyModel
        include ::Mongoid::Document
        store_in collection: 'buggy_models'
      
        field :list, type: :array, default: []
      end
      

      Create one element:

      ::BuggyModel.create!({ _id: 'test' })
      

      Add something to the set:

      ::BuggyModel.update_all({
        '$addToSet': {
          list: 'a',
        },
      })
      

      Reload:

      ::BuggyModel.find('test').list
      

      Expecting: ['a']
      Actual: [nil]

      Why?

      Digging further, the problem is in mongoize of Array fields that was called from hash._consolidate_.

      The workaround exists by omitting the type of the field. Changing this:

      field :list, type: :array, default: []
      

      To this:

      field :list, default: []
      

      Probably Mongoid::Persistable::LIST_OPERATIONS handling needs revises.

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            jamis.buck@mongodb.com Jamis Buck
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: