MongoDB $unwind多个字段并按时间戳排序


MongoDB $unwind more than one field and sort by timestamp

我在那个查询中创建了一个查询在那个查询中我使用了多个字段$unwind。查询回复很好,但我的前辈建议我这是错误的查询。因为查询响应的格式不正确,并且不是r_y_b_phasetimestamp时间戳的升序。我给出的查询如下:

 db.energy.aggregate([
    { 
        $project: { EventTS: 1, RPhaseVoltage: 1, YPhaseVoltage:1, BPhaseVoltage:1} 
    },
    {
        $unwind: {
            path: "$RPhaseVoltage",
            includeArrayIndex: "arrayIndex",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $unwind: {
            path: "$YPhaseVoltage",
            includeArrayIndex: "arrayIndex",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $unwind: {
            path: "$BPhaseVoltage",
            includeArrayIndex: "arrayIndex",
            preserveNullAndEmptyArrays: true
        }
    },
    {
      $match: {
            $and: [ 
                {RPhaseVoltage: {$ne: null}},
                {YPhaseVoltage: {$ne: null}},
                {BPhaseVoltage: {$ne: null}},
            ]
        }
    },
    {
        $project: {
            _id:0,
            EventTS:1,
            RPhaseVoltage: 1,
            YPhaseVoltage: 1,
            BPhaseVoltage:1,
            r_y_b_phasetimestamp: {
                "$add": [
                    { "$subtract": ["$EventTS", new Date("1970-01-01")]},
                    { "$multiply": [ 60000, "$arrayIndex" ] }
                ]
            }
        }
    },
    {
      $project: {
        "rvoltage_data":["$r_y_b_phasetimestamp", "$RPhaseVoltage"],
        "yvoltage_data":["$r_y_b_phasetimestamp", "$YPhaseVoltage"],
        "bvoltage_data":["$r_y_b_phasetimestamp", "$BPhaseVoltage"]
      }
    },
    {
        $sort:{
            r_y_b_phasetimestamp:-1
        }
    }
]);

我的收藏样本如下

    { 
    "_id" : ObjectId("57742e0f8d8b8fdf278b45d1"), 
    "EventTS" : ISODate("2016-06-24T12:30:00.000+0000"), 
    "RPhaseVoltage" : [ 
        null, 
        231.81, 
        231.81, 
        null, 
        231.42
    ], 
    "YPhaseVoltage" : [ 
        229.95, 
        229.95, 
        null, 
        null, 
        231.32
    ], 
    "BPhaseVoltage" : [
        null, 
        231.44, 
        231.44, 
        null, 
        null
    ]
}

我的图形输出链接是图形绘制链接。这个查询我正在使用的图形绘制。请告诉我我的查询是错误的,为什么图形是这样绘制的

您使用$project 2次。当您使用$project时,只有投影值被传递到管道的下一部分。在使用排序之前,您不投射r_y_b_phasetimestamp。因此,排序时r_y_b_phasetimestamp是未定义的,将导致未排序的结果。尝试在"bvoltage_data":["$r_y_b_phasetimestamp", "$BPhaseVoltage"]之后通过r_y_b_phasetimestamp:"$r_y_b_phasetimestamp"。最后的代码是

db.energy.aggregate([
{ 
    $project: { EventTS: 1, RPhaseVoltage: 1, YPhaseVoltage:1, BPhaseVoltage:1} 
},
{
    $unwind: {
        path: "$RPhaseVoltage",
        includeArrayIndex: "arrayIndex",
        preserveNullAndEmptyArrays: true
    }
},
{
    $unwind: {
        path: "$YPhaseVoltage",
        includeArrayIndex: "arrayIndex",
        preserveNullAndEmptyArrays: true
    }
},
{
    $unwind: {
        path: "$BPhaseVoltage",
        includeArrayIndex: "arrayIndex",
        preserveNullAndEmptyArrays: true
    }
},
{
  $match: {
        $and: [ 
            {RPhaseVoltage: {$ne: null}},
            {YPhaseVoltage: {$ne: null}},
            {BPhaseVoltage: {$ne: null}},
        ]
    }
},
{
    $project: {
        _id:0,
        EventTS:1,
        RPhaseVoltage: 1,
        YPhaseVoltage: 1,
        BPhaseVoltage:1,
        r_y_b_phasetimestamp: {
            "$add": [
                { "$subtract": ["$EventTS", new Date("1970-01-01")]},
                { "$multiply": [ 60000, "$arrayIndex" ] }
            ]
        }
    }
},
{
  $project: {
    "rvoltage_data":["$r_y_b_phasetimestamp", "$RPhaseVoltage"],
    "yvoltage_data":["$r_y_b_phasetimestamp", "$YPhaseVoltage"],
    "bvoltage_data":["$r_y_b_phasetimestamp", "$BPhaseVoltage"],
    r_y_b_phasetimestamp:"$r_y_b_phasetimestamp"
  }
},
{
    $sort:{
        r_y_b_phasetimestamp:-1
    }
}
]);