-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
I ran into a little issue this morning with aggregation sum function.
I have some Notes that haven't yet the :attachments_file_size field persisted (the spirit of documents DB).
And when I run :
<%= debug Note.all.sum(:attachments_file_size) %>
it renders :
— !!null
...
As a workaround, I have to modifiy the lib/mongoid/javascript/functions.yml to take account of uninitialized field access.
I did the max & min function too (if the prev is 'start' and the field is uninitialized, it lets 'start')
max: "function(obj, prev) { if (obj.[field] && prev.max == 'start') { prev.max = obj.[field]; } if (obj.[field] && prev.max < obj.[field]) { prev.max = obj.[field]; } }" min: "function(obj, prev) { if (obj.[field] && prev.min == 'start') { prev.min = obj.[field]; } if (obj.[field] && prev.min > obj.[field]) { prev.min = obj.[field]; } }" sum: "function(obj, prev) { if (prev.sum == 'start') { prev.sum = 0; } if (obj.[field]) { prev.sum += obj.[field]; } }"
Note: that doesn't break avg because the nil fields are already counted and (6 + nil ) / 2 is expected to be 3.
Can we have this for 2.3.3 ? I would be nice as it is a quick fix.