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

I have the following query working in mongoDB but its not working in PHP. MongoDB Query

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

Above query is converted to 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
                                ]
                        ]
                    ]);
                });

I am getting following error

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

What is problem in my converted php query? Please suggest resolution of above problem.

You should always convert normal query to array decode. json_decode should make query for PHP driver and json_encode should give query mongodb query parameters.

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

Like this :

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,
      )
   )
)

If you have at least PHP5.4, you can use simpler array syntax. Replace array( with [ and ) with ] for array.

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