Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-23347

Date difference operator

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Query Optimization

      It would be really nice to have a flexible date difference operator that calculates the difference between two dates resulting in a duration (e.g. duration in msecs, secs, mins, hours, etc).

      Consider the following projection stage in an aggregation pipeline. I need to calculate the difference in days between two dates - and one date may be null.

        
      { $project: {
        DIM: {
            $floor: {
              $divide: [
                {$subtract: [ {$ifNull: [ '$DDAT', new Date()]}, '$FDAT' ]},
                86400000
              ]
            }
          }
        }
      }
      

      This works - and it's fast - but it would be more pleasant if I could specify something like:

      { $project: {
        DIM: {
            $diff: [ {$ifNull: [ '$DDAT', new Date()]}, '$FDAT', 'd' ]
          }
        }
      }
      

      ArangoDB offers a date difference function which is pretty nice,

      DATE_DIFF(date1, date2, unit, asFloat): Calculate the difference between two dates in given time unit, optionally with decimal places. Returns a negative value if date1 is greater than / after date2.

      unit can be either of the following to specify the time unit to return the difference in (case- insensitive):

      y, year, years
      m, month, months
      w, week, weeks
      d, day, days
      h, hour, hours
      i, minute, minutes
      s, second, seconds
      f, millisecond, milliseconds

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            jamesmoger James Moger
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: