MongoDB查询转换为PHP?

时间:2016-07-22 09:03:29

标签: php mongodb mongodb-query aggregation-framework

我无法将其转换为PHP。我的查询是在mongodb中工作并获得良好的输出。但同样的查询我试图转换成PHP显示错误

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

我的收集样本如下

    { 
    "_id" : ObjectId("5790f9407ffbe031fa2049e3"), 
    "EventTS" : ISODate("2016-07-01T04:30:00.000+0530"), 
    "KeyValues" : [
        {
            "InputVolt" : null
        }, 
        {
            "InputVolt" : 250
        }, 
        {
            "InputVolt" : 230 
        }, 
        {
            "InputVolt" : 230
        }, 
        {
            "InputVolt" : 240 
        }
    ]
},

{ 
    "_id" : ObjectId("5790f9407ffbe031fa2049e4"), 
    "EventTS" : ISODate("2016-07-01T04:35:00.000+0530"),
    "KeyValues" : [
        {
            "InputVolt" : null
        }, 
        {
            "InputVolt" : 260
        }, 
        {
            "InputVolt" : null
        }, 
        {
            "InputVolt" : null
        }, 
        {
            "InputVolt" : null
        }
    ]
}

我的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}}
    ]
  }
}
);

我的mongo查询转换为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);

我不知道发生了什么。请建议我。

0 个答案:

没有答案