I already have a solution but I am looking at a solution which does all the job on MongoServer (Because I think it'd be faster and less memory consuming)
I have a Class Method like:
function getTotalOutstandingAmount(){
$outstandingAmount = 0;
$subs = $this->mongo->selectCollection('SmsSubscriptions');
$activeSubsctiptions = $subs->find(array('Status' => 1, '$where' => "this.SubscriptionPayments < this.SubscriptionTotal"));
foreach ($activeSubsctiptions AS $sub){
$outstandingAmount += $sub['SubscriptionTotal'] - $sub['SubscriptionPayments'];
}
return $outstandingAmount;
}
Now is there a way of calculating the Sum of differences of the two fields using aggregate
method of MongoDB? Is there any other more efficient way of doing this?