Each document in my collection contains two values that are structured as follows:
{
originalPrice: 500,
ourPrice: 420
}
I would like users to be able to sort by the savings they can make when shopping from us instead of a competitor, which in this case is 80.
However, since the saveAmount value is not stored in the database directly, I cannot simply perform a sort operation like this:
Goods.find({}, {sort: saveAmount: 1})
Inserting this number into the database could be a simple solution, but I am looking for an alternative approach unless it is too complex.
Therefore, I need a function like this:
var saveAmount = function(originalPrice, ourPrice) {
return originalPrice - ourPrice
}
I want to utilize this function to sort the data. How can I achieve this?