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

Better semantics for $add and $subtract with Dates

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.3.2
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • Fully Compatible

      Currently we treat numbers added or subtracted to dates as whole days (24*60*60*1000 millis). I think we should treat them as milliseconds instead. This way adding 30 minutes looks like {$add:['$date', 30*60*1000]} rather than {$add:['$date', 30.0/24/60]} or {$add:['$date', 1.0/24/2]}

      Also currently subtracting two dates yields a Date-typed result when the result should probably be a NumberLong of the number of milliseconds between the dates. Currently it would return a result around 1970-01-01 for dates that are near each other.

      Expected results

      Date - Date : NumberLong(Milliseconds between times)
      Num - Date : Error
      Date - Num : Date(original - Num as milliseconds)

      Date + Date : Error (current behavior)
      Num + Date : Date(with num treated as millis)
      Date + Num : Same as above.

            Assignee:
            mathias@mongodb.com Mathias Stearn
            Reporter:
            mathias@mongodb.com Mathias Stearn
            Votes:
            18 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: