首页 > 解决方案 > 如何从 laravel mongodb 中的原始表达式中获取结果?

问题描述

我正在使用 Laravel 和 Mongodb。我想从原始表达式中获取结果但失败了。

我试图将结果转换为数组。结果数据显示,但我不确定这是否是正确的方法。

下面是我的代码示例:

$result = DB::collection($tbl_name)->raw(function($collection) {
    return $collection->aggregate([
        [
            '$match' => [
                'field_1' => [
                    '$regex' => 'some value here'
                ]
            ]
        ],
        [
            '$group' => [
                '_id' => '$field_2',
                'total_amount' => ['$sum' => ['$toDouble' => '$field_3']],
                'total_count' => ['$sum' => 1],
            ]
        ]
    ]);
});

如果我使用将其转换为数组,这是我得到的结果$result->toArray()

Array
(
    [0] => MongoDB\Model\BSONDocument Object
        (
            [storage:ArrayObject:private] => Array
                (
                    [_id] =>
                    [total_amount] => 1000.0000
                    [total_count] => 100
                )

        )

)

我希望有另一种方法来检索结果。谢谢。

标签: phplaravelmongodb

解决方案


推荐阅读