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

Distinguish between nil and "unset" in DB (propose to introduce Mongoid::Undefined)

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 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?)

            Assignee:
            Unassigned Unassigned
            Reporter:
            johnnyshields Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: