-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
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.
- links to