使用Laravel和Mongodb将毫秒转换为时间戳

时间:2016-06-10 07:05:14

标签: php mongodb laravel laravel-5.2

我的mongodb中有超过10000条记录。现在我不想使用php处理代码,因此我在laravel 5中创建了以下查询,它在mongodb中执行所有处理并非常快速地生成结果。 查询如下:

 $result = Role::raw(function ($collection){
        return $collection->aggregate(array(
            array( '$match' => array( "PanelID" => "A00898" ) ),
            array( '$project' => array( 'EventTS' => 1, 'MainsPower' => 1, '_id' => 0 ) ),
            array(
                '$unwind' => array(
                    'path' => '$MainsPower',
                    'includeArrayIndex' => "arrayIndex",
                    'preserveNullAndEmptyArrays' => true
                )
            ),
            array(
                '$project' => array(
                    'MainsPower' => 1,
                    'timestamp' => array(
                        '$add' => array(
                            '$EventTS',
                            array( '$multiply' => array( 60000, '$arrayIndex' ) )
                        )
                    )
                )
            )
        ));
    })->toArray();

以上查询以下列方式生成结果

    Array
(
    [0] => Array
        (
            [MainsPower] => 255
            [timestamp] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1448911800000
                )

        )

    [1] => Array
        (
            [MainsPower] => 0
            [timestamp] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1448911860000
                )

        )

    [2] => Array
        (
            [MainsPower] => 0
            [timestamp] => MongoDB\BSON\UTCDateTime Object
                (
                    [milliseconds] => 1448911920000
                )

        )
}

但我希望以下列方式回复

    Array
(
    [0] => Array
        (
            [MainsPower] => 255
            [timestamp] => 2015-10-07T14:28:41.545Z


        )

    [1] => Array
        (
            [MainsPower] => 0
            [timestamp] => 2015-10-15T14:28:41.545Z
                )

        )

    [2] => Array
        (
            [MainsPower] => 0
            [timestamp] => 2015-11-07T14:28:41.545Z


        )
}

我尝试了许多解决方案并在laravel中使用以转换日期格式,但它会出现以下错误

MongoDB\BSON\UTCDateTime not found

0 个答案:

没有答案