2016-07-22 102 views
1

我的查詢是在mongodb中工作並正確生成輸出。但相同的查詢,我試圖用PHP來運行,那麼它提供了以下錯誤如何將mongoDB查詢轉換爲PHP?

Array 
    (
     [ok] => 0 
     [errmsg] => A pipeline stage specification object must contain exactly one field. 
     [code] => 16435 
    ) 

以下是我的MongoDB查詢

db.sample_coll.aggregate(
{ 
    $unwind: { 
     path:"$KeyValues", 
     includeArrayIndex:"arrayIndex", 
     preserveNullAndEmptyArrays:true 
    } 
}, 
{ 
    $project: { 
     timestamp:{ 
      "$add":["$EventTS",{"$multiply":[60000,"$arrayIndex"]}] 
     } , 
     "InputVolt":"$KeyValues.InputVolt", 
     arrayIndex:1 
     } 
}, 
{ 
    $match: { 
    $and: [ 
     {InputVolt: {$ne: null}} 
    ] 
    } 
} 
); 

上面的查詢轉換在PHP

$pipeline = array(
      array('$unwind' => 
        array('path' => '$KeyValues'), 
        array('includeArrayIndex' => 'arrayIndex'), 
        array('preserveNullAndEmptyArrays' => 'true') 
      ), 
      array(
       '$project' => array(
        'timestamp' => array(
         '$add' => array(
          '$EventTS', 
          array('$multiply' => array(60000, '$arrayIndex')) 
         ) 
        ), 
        array('InputVolt' => array('$KeyValues', 'InputVolt' )) , 
        array('arrayIndex' => 1) 
       ) 
      ), 
      array(
       '$match' => array(
        '$and' => array(
         array('InputVolt' => array('$ne' => null)), 
        ) 
       ) 
      ) 

); 
$result = $collection->aggregate($pipeline); 

請幫助解決這個問題。 在此先感謝

回答

1

你的管道在PHP中:

$pipeline = array(
     array('$unwind' => array(
      'path' => '$KeyValues', 
      'includeArrayIndex' => 'arrayIndex', 
      'preserveNullAndEmptyArrays' => true 
     )), 
     array('$project' => array(
      'timestamp' => array(
       '$add' => array('$EventTS', array('$multiply' => array(60000, '$arrayIndex'))) 
      ), 
      'InputVolt' => '$KeyValues.InputVolt', 
      'arrayIndex' => 1 
     )), 
     array('$match' => array(
      'InputVolt' => array('$ne' => null) 
     )) 
    );