我已经有了一个解决方案,但我正在寻找一个可以在 MongoServer 上完成所有工作的解决方案(因为我认为它会更快且内存消耗更少)
我有一个类方法,如:
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;
}
aggregate
现在有没有一种方法可以使用MongoDB 的方法计算两个字段的差异之和?还有其他更有效的方法吗?