使用聚合将MongoDB查询转换为PHP驱动程序聚合查询

时间:2016-07-25 07:27:22

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

我有以下查询在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查询有什么问题?请建议解决上述问题。

1 个答案:

答案 0 :(得分:0)

您应该始终将普通查询转换为数组解码。 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
      ]
   ]
]
相关问题