I want to calculate avgAmount as the sum 3 fields = [balanceLoaded + operatorDueAmount - walletUsage].
But these fields can either be zero or themselves (balanceLoaded, operatorDueAmount, walletUsage) depending on the condition. I want to sum up the final value which is calculated after applying the condition. And yes I need to subtract the walletUsage from other fields. Can someone guide me on how to do that without changing the names of fields in the project pipeline?
const result = await Bus.aggregate(
[
{
$match: { ...filter },
},
{
$project: {
_id: 1,
Amount1: '$Amount1',
Amount2: { $cond: [{ $in: ['example1', dueFieldsConfigs] }, '$Amount2', 0] },
Amount3: { $cond: [{ $in: ['example2', dueFieldsConfigs] }, '$Amount3', 0] },
Amount4: { $cond: [{ $in: ['example3', dueFieldsConfigs] }, '$Amount4', 0] },
avgAmount: { $sum: ['$Amount1', '$Amount2', '$Amount3'] },
},
},
{ $sort: sort },
{ $skip: skip },
{ $limit: limit },
],
);