如何得到 MongoDB 中两个字段的差值之和?
How to get sum of differences of two fields in MongoDB?
我已经有了一个解决方案,但我正在寻找一个可以在 MongoServer 上完成所有工作的解决方案(因为我认为它会更快,占用的内存更少)
我有一个 Class 方法,例如:
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;
}
现在有没有一种方法可以使用MongoDB的aggregate
方法计算两个字段的差值之和?还有其他更有效的方法吗?
聚合方法应该有这个管道:
db.SmsSubscriptions.aggregate([
{
"$project": {
"outstandingAmount": {
"$subtract": ["$SubscriptionTotal", "$SubscriptionPayments"]
},
"Status": 1
}
},
{ "$match": { "Status": 1, "outstandingAmount": { "$gt": 0 } } },
{
"$group": {
"_id": null,
"totalOutstandingAmount": { "$sum": "$outstandingAmount" }
}
}
])
等效的PHP示例实现:
$ops = array(
array(
"$project" => array(
"Status" => 1,
"outstandingAmount" => array(
"$subtract" => array("$SubscriptionTotal", "$SubscriptionPayments")
)
)
)
),
array(
"$match" => array(
"Status" => 1,
"outstandingAmount" => array("$gt" => 0)
)
),
array(
"$group" => array(
"_id" => null,
"totalOutstandingAmount" => array("$sum" => "$outstandingAmount" )
)
)
);
$results = $this->mongo->selectCollection('SmsSubscriptions')->aggregate($ops);
求差和的通用简单紧凑解决方案
收藏X
{ _id: 'xxxx', itemOne: 100, itemTwo: 300 }
{ _id: 'yyyy', itemOne: 200, itemTwo: 800 }
{ _id: 'zzzz', itemOne: 50, itemTwo: 400 }
聚合运算
db.collectionX.aggregate([
{
$group: {
_id: null,
sumOfDifferences: { $sum: { $subtract: ['$itemTwo', '$itemOne']}
}
])
回应
{
"_id" : null,
"sumOfDifferences" : 1150
}
我已经有了一个解决方案,但我正在寻找一个可以在 MongoServer 上完成所有工作的解决方案(因为我认为它会更快,占用的内存更少)
我有一个 Class 方法,例如:
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;
}
现在有没有一种方法可以使用MongoDB的aggregate
方法计算两个字段的差值之和?还有其他更有效的方法吗?
聚合方法应该有这个管道:
db.SmsSubscriptions.aggregate([
{
"$project": {
"outstandingAmount": {
"$subtract": ["$SubscriptionTotal", "$SubscriptionPayments"]
},
"Status": 1
}
},
{ "$match": { "Status": 1, "outstandingAmount": { "$gt": 0 } } },
{
"$group": {
"_id": null,
"totalOutstandingAmount": { "$sum": "$outstandingAmount" }
}
}
])
等效的PHP示例实现:
$ops = array(
array(
"$project" => array(
"Status" => 1,
"outstandingAmount" => array(
"$subtract" => array("$SubscriptionTotal", "$SubscriptionPayments")
)
)
)
),
array(
"$match" => array(
"Status" => 1,
"outstandingAmount" => array("$gt" => 0)
)
),
array(
"$group" => array(
"_id" => null,
"totalOutstandingAmount" => array("$sum" => "$outstandingAmount" )
)
)
);
$results = $this->mongo->selectCollection('SmsSubscriptions')->aggregate($ops);
求差和的通用简单紧凑解决方案
收藏X
{ _id: 'xxxx', itemOne: 100, itemTwo: 300 }
{ _id: 'yyyy', itemOne: 200, itemTwo: 800 }
{ _id: 'zzzz', itemOne: 50, itemTwo: 400 }
聚合运算
db.collectionX.aggregate([
{
$group: {
_id: null,
sumOfDifferences: { $sum: { $subtract: ['$itemTwo', '$itemOne']}
}
])
回应
{
"_id" : null,
"sumOfDifferences" : 1150
}