-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
2
I think this was raised in the past (https://github.com/mongoid/mongoid/issues/647), but currently there's no way to distinguish between "nil" and "unset in db". This is particularly useful for sparse indices, which will consider "null" to be a value for index purposes (contrary to user intention.)
Not sure if this is possible, but it would be very nice if we could introduce a constant Mongoid::Undefined < NilClass which, if used would guarantee a field stays unset in the DB (not set to null)
It might be also nice to have Mongoid::UndefinedArray < Array and Mongoid::UndefinedHash which are handled as unset in DB whenever they are equal to an empty array / empty hash respectively in Ruby.
More broadly, you could make a module Mongoid::UnsetIfBlank which could be included into any primitive, which would unset the field in the DB if self.blank? is true
(Maybe this is better to raise with BSON driver?)
- duplicates
-
MONGOID-4611 Option to unset nil-valued fields in DB
- Backlog
- is related to
-
MONGOID-4592 Support the :local_default or :store_default option to fields, which defines a default value for document fields without persisting that value to the database
- Closed