【问题标题】:Converting MongoDB query using aggregate to PHP driver aggregate query将使用聚合的 MongoDB 查询转换为 PHP 驱动程序聚合查询
【发布时间】:2016-07-25 07:27:21
【问题描述】:

我有以下查询在 mongoDB 中工作,但在 PHP 中不工作。 MongoDB查询

db.energy_meter.aggregate(
{
  $unwind: {
        path:"$KeyValues", 
        includeArrayIndex:"arrayIndex", 
        preserveNullAndEmptyArrays:true 
    }
},
{
  $project: {
        timestamp:{ 
          "$add":["$EventTS",{"$multiply":[60000,"$arrayIndex"]}] 
        } ,
        "RPhaseVoltage":"$KeyValues.RPhaseVoltage",
        arrayIndex:1,
        }
}
);

以上查询转换为PHP

 $cursor = DB::collection('energy_meter')->raw(function($collection)
                {
                    return $collection->aggregate([                   
                        [ 
                            '$unwind' => 
                                ['path' => '$KeyValues'],
                                ['includeArrayIndex' => 'arrayIndex'],
                                ['preserveNullAndEmptyArrays' => 'true']
                        ],

                        [ 
                            '$project' => 
                                [
                                    'timestamp' => [
                                        '$add' => [
                                                '$EventTS',
                                                ['$multiply' => [60000, '$arrayIndex']]
                                        ]
                                    ]
                                ],
                                [
                                    'MainsInputVoltagev' => ['$KeyValues.MainsInputVoltagev']
                                ],
                                [
                                    'arrayIndex' => 1
                                ]
                        ]
                    ]);
                });

我收到以下错误

RuntimeException in Aggregate.php line 168: A pipeline stage specification object must contain exactly one field.

转换后的 php 查询有什么问题?请提出上述问题的解决方案。

【问题讨论】:

    标签: php mongodb laravel mongodb-query laravel-5.2


    【解决方案1】:

    您应该始终将普通查询转换为数组解码。 json_decode 应该对 PHP 驱动程序进行查询,而 json_encode 应该提供查询 mongodb 查询参数。

    (
    {
      $unwind: {
            path:"$KeyValues", 
            includeArrayIndex:"arrayIndex", 
            preserveNullAndEmptyArrays:true 
        }
    },
    {
      $project: {
            timestamp:{ 
              "$add":["$EventTS",{"$multiply":[60000,"$arrayIndex"]}] 
            } ,
            "RPhaseVoltage":"$KeyValues.RPhaseVoltage",
            arrayIndex:1,
            }
      }
     )
    

    像这样:

    array(
      array(
         '$unwind' => array(
            'path' => '$KeyValues', 
            'includeArrayIndex' =>"arrayIndex", 
            'preserveNullAndEmptyArrays'=> true 
         )
       ),
      array(
        '$project' => array(
            'timestamp' => array( 
              '$add'=>[ '$EventTS',array('$multiply'=>[60000,'$arrayIndex'])] 
            ) ,
            "RPhaseVoltage" => '$KeyValues.RPhaseVoltage',
            'arrayIndex' =>1,
          )
       )
    )
    

    如果您至少有 PHP5.4,则可以使用更简单的数组语法。将array( 替换为[,将) 替换为] 用于数组。

    [                   
       [ 
          '$unwind' => [
               'path' => '$KeyValues',
               'includeArrayIndex' => 'arrayIndex',
               'preserveNullAndEmptyArrays' => 'true'
          ]
       ],
       [ 
          '$project' => [
                'timestamp' => [
                    '$add' => [
                            '$EventTS',
                            [ '$multiply' => [60000, '$arrayIndex'] ]
                    ]
                ],
                'MainsInputVoltagev' => '$KeyValues.MainsInputVoltagev',
                'arrayIndex' => 1
          ]
       ]
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-09
      • 2016-02-11
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2017-01-25
      相关资源
      最近更新 更多