如何使用MongoDB在$ unwind中使用多个数组索引?

时间:2016-06-30 13:17:43

标签: mongodb mongodb-query

我有以下查询,我在三个字段使用相同的数组索引。但由于使用相同的arra索引,查询响应不正确。使用查询如下

$pipeline = array(
        array(
            '$project' => array(
                'YPhaseVoltage' => 1,
                'RPhaseVoltage' => 1,
                'EventTS' => 1
            )
        ),          
        array(
            '$unwind' => array(
                'path' => '$RPhaseVoltage',
                'includeArrayIndex' => "arrayIndex",
                'preserveNullAndEmptyArrays' => true
            )
        ),
        array(
            '$unwind' => array(
                'path' => '$YPhaseVoltage',
                'includeArrayIndex' => "arrayIndex",
                'preserveNullAndEmptyArrays' => true
            )
        ),
        array('$match' => array(
                '$and' => array(
                    array("RPhaseVoltage" => array('$ne' => null)),
                    array("YPhaseVoltage" => array('$ne' => null))
                )
    ),
        ),
        array(
            '$project' => array(
                '_id' => 0,
                'RPhaseVoltage' => 1,
                'YPhaseVoltage' => 1,
                'RYBTimestamp' => array(
                    '$add' => array(
                        array('$subtract' => array('$EventTS', $default_date)),
                        array('$multiply' => array( 60000, '$arrayIndex' )) 
                    )
                )
            )
        ),
        array(
            '$project' => array(
                'rvoltage_data' => array('$RYBTimestamp', '$RPhaseVoltage'),
                'yvoltage_data' => array('$RYBTimestamp', '$YPhaseVoltage'),
                'RYBTimestamp' => 1
            )
        ),
        array(
            '$sort' => array('RYBTimestamp' => 1),
        ),
        array(
            '$limit' => 50
        )
);

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

我想为每个字段使用单独的数组索引。这给了我预期的回应。请教我如何在mongodb聚合中使用多个数组索引?

0 个答案:

没有答案