I need to calculate weighted average over a set of documents. Each document contains both a weight and a value in a separate field. Here is an example: I have following 2 documents representing a sales transaction.
{
quantity : 3,
price : 2
}
{
quantity : 9,
price : 6
}
I want to find the average price for both transactions. This is a weighted average where weight is the quantity and value is the price. This can be calculated by
AveragePrice = (3 * 2 + 9 * 6 ) / (3 + 9)
.
How do I perform this calculation using aggregation framework?