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

sum and avg return NaN if the field doesn't exist

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      sum/avg will return either nil, NaN or the resulting number. If any of the records that are being map/reduced don't have the field you're working on, it will always return NaN regardless. It's trivial to work around, but it seems like Mongoid should be checking to make sure the field is at least undefined/nil before trying to use it.

      class Foobar
      include Mongoid::Document
      field :pending, :type => Integer
      end

      Foobar.create(:pending => 5)
      Foobar.create(:pending => nil)
      Foobar.sum(:pending) # NaN

            Assignee:
            Unassigned Unassigned
            Reporter:
            zanker Zachary Anker
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: