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

Add Weighted Average method to aggregation framework

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.0
    • Component/s: Aggregation Framework
    • Query Optimization

      Add '$weightedAverage' operator to the aggregation framework.

      Currently, MongoDB has the $avg operator. It simplify the calculation of simple averages. The request is to create built in operator for calculating weighted average - where the weight of each item is specified by a different field.

      Weighted average is used extensively in financial, statistical and scientific analysis of large data set. While it is technically possible to implement it based on existing operators (sum(...)), the implementation is error-prone, and correct handling of missing/non-numeric values make is difficult.

      Proposed operator: $weightedAvg($weight, $value)

      Should calculation

      RESULT = (w1*v1 + w2*v2 + w3*v3 ... + w(n)*v(n)) / (w1 + w2 + w3 + ... + W(n))
      

      Calculation should ignore any record where w(I), or v(I) is not numeric. this is important, as the current replacement $sum($multiply($v1, $w1)) / sum($w1) does not handle missing values correctly, and require complex filtering.

      If possible, it will be nice to allow for multiple values to be calculated with the same pass.
      $weightedAvg($weight, $v1, $v2, $v3) will return the weighted average of the specific value list, weighted by the specific weight, similar to the ability to perform sum/average on multiple values.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            yair.lenga@gmail.com Yair Lenga
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: