当前位置:  开发笔记 > 编程语言 > 正文

Laravel 5 mongo DB的倍增列的Eloquent总和

如何解决《Laravel5mongoDB的倍增列的Eloquent总和》经验,为你挑选了1个好方法。



1> Alex Blex..:

我相信聚合运算符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']]
                    ]
                ]
            ]
        ]);
    })

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有