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

Date difference operator

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor - P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: Aggregation Framework
    • Labels:
      None

      Description

      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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              backlog-query-optimization Backlog - Query Optimization
              Reporter:
              jamesmoger James Moger
              Participants:
              Votes:
              1 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Created:
                Updated: