我相信聚合运算符sum
会将精确的列名称作为参数.您可以先尝试project
乘法,然后对结果求和:
DB::connection($this->MongoSchemaName) ->collection($this->InvoicesTable) ->where('ContactID', (int)$customer->ContactID) ->project([ 'ContactID' => 1, 'TotalInBaseCurrency' => ['$multiply' => ['$Total', '$CurrencyRate']] ]) ->sum('TotalInBaseCurrency')
或直接使用聚合:
DB::connection($this->MongoSchemaName) ->collection($this->InvoicesTable) ->raw(function($collection) use ($customer){ return $collection->aggregate([ ['$match' => [ 'ContactID' => (int)$customer->ContactID, 'Type' => 'PAYMENT' ] ], ['$group' => [ '_id' => '$ContactID', 'TotalInBaseCurrency' => [ '$sum' => ['$multiply' => ['$Total', '$CurrencyRate']] ] ] ] ]); })