执行查询后

$orders = Orders::connection()->connection->command(array(
  'aggregate' => 'orders',
  'pipeline' => array(
    array(
      '$group' => array(
        '_id' => array('cust_id'=>'$cust_id','ord_date'=>'$ord_date'),
      'total' => array( '$sum' => '$price') ,
        ),
      ),
    )
));

我得到$orders的输出数组
Array
(
    [0] => Array
        (
            [_id] => Array
                (
                    [cust_id] => xyz123
                    [ord_date] => MongoDate Object
                        (
                            [sec] => 1351875851
                            [usec] => 102000
                        )

                )

            [total] => 75
        )

    [1] => Array
        (
            [_id] => Array
                (
                    [cust_id] => abc123
                    [ord_date] => MongoDate Object
                        (
                            [sec] => 1351875851
                            [usec] => 102000
                        )

                )

            [total] => 125
        )

)

我已尝试使用MongoDate对象正确打印它
foreach($orders['result'] as $o){
    $date = $o['ord_date'];
    echo date('Y-M-d',$date->sec);
}

也试过了
foreach($orders['result'] as $o){
    $date = new MongoDate ($o['ord_date']);
    echo date('Y-M-d',$date->sec);
}

有人能告诉我正确的方法吗?

最佳答案

您缺少对象的_id级别。请改为:

foreach($orders['result'] as $o){
    $date = $o['_id']['ord_date'];
    echo date('Y-M-d',$date->sec);
}

关于php - Mongodate转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13418309/

10-13 02:13