Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获得MongoDB中两个字段的差值之和?_Php_Mongodb_Aggregation Framework_Mongodb Aggregation - Fatal编程技术网

Php 如何获得MongoDB中两个字段的差值之和?

Php 如何获得MongoDB中两个字段的差值之和?,php,mongodb,aggregation-framework,mongodb-aggregation,Php,Mongodb,Aggregation Framework,Mongodb Aggregation,我已经有了一个解决方案,但我正在寻找一个在MongoServer上完成所有工作的解决方案(因为我认为它会更快,占用更少内存) 我有一个类方法,如: function getTotalOutstandingAmount(){ $outstandingAmount = 0; $subs = $this->mongo->selectCollection('SmsSubscriptions'); $activeSubsctiptions = $subs->fin

我已经有了一个解决方案,但我正在寻找一个在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;
}
函数getTotalOutstandingAmount(){ $未结清金额=0; $subs=$this->mongo->selectCollection('SmsSubscriptions'); $activesubscriptions=$subs->find(数组('Status'=>1',$where'=>“this.SubscriptionPayments
现在有没有一种方法可以使用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);

求差分之和的一般简单紧致解

收藏品

{ _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
}

工作完美。我想知道如果
$subtract
中的一个或两个字段都是字符串,该如何工作?唯一不幸的是
$subtract
不能很好地处理字符串,因为运算符的参数可以是任何有效的表达式,只要它们解析为数字和/或日期。因此,您需要一种机制来将字符串转换为某种数字表示,这样才能起作用。