Using project fields in calculation on mongoose
Yes it's possible with the aggregation framework. You can either use the aggregate()
pipeline builder as:
Model.aggregate() .project({ "percentage_amount": { "$multiply": [ "$commission", { "$divide": ["$price", 100] } ] }, "payable": { "$subtract": ["$price", percentage_amount] } }) .exec(function(err, result) { if (err) return handleError(err); console.log(result) });
or using the aggregate()
operator pipeline array:
Model.aggregate([ { "project": { "percentage_amount": { "$multiply": [ "$commission", { "$divide": ["$price", 100] } ] }, "payable": { "$subtract": ["$price", percentage_amount] } }]).exec(function(err, result) { if (err) return handleError(err); console.log(result)});