汇总并分组以从Mongo数据库中的集合中获取最后一行

时间:2018-07-09 20:16:02

标签: mongodb aggregation-framework

从下面的mongo文档中,我想获取versions.errors中的最后一条记录。 从下面的文档中,我只希望消息显示为“带有与priceZone = AMMO相关的FERC信用标志的NITS保留数为0,该值应等于1”的最后一级,消息和时间戳。

{
    "_id" : {
        "objectIdentifier" : {
            "identifier" : {
                "settlementInstanceId" : "3b37de20-e47e-435c-896e-8d9dea0eb63a"
            },
            "objectName" : "SinkPriceZonesWithFercCreditFlag"
        },
        "settlementInstanceId" : "3b37de20-e47e-435c-896e-8d9dea0eb63a",
        "validator" : "SinkPriceZoneFercCreditFlagValidator",
        "@objectName" : "ValidationResult"
    },
    "Versions" : [ 
        {
            "errors" : [ 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= ALTE are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }, 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= EMTO are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }, 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= ENTO are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }, 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= IPL are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }, 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= ETTO are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }, 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= OTP are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }, 
                {
                    "level" : "WARN",
                    "message" : "The number of NITS reservations with FERC credit flag associated with priceZone= AMMO are 0, this value should be equal to 1",
                    "timestamp" : NumberLong(1531167099204)
                }
],
            "resultSetId" : "56170750-8f1c-4c6a-9ec4-d4fdcfbd8c03",
            "scope" : "COMPLETENESS",
            "validationLevelSeverity" : "WARN",
            "validationResultsIdentifier" : {
                "identifier" : {
                    "objectIdentifier" : {
                        "identifier" : {
                            "settlementInstanceId" : "3b37de20-e47e-435c-896e-8d9dea0eb63a"
                        },
                        "objectName" : "SinkPriceZonesWithFercCreditFlag"
                    },
                    "settlementInstanceId" : "3b37de20-e47e-435c-896e-8d9dea0eb63a",
                    "validator" : "SinkPriceZoneFercCreditFlagValidator"
                },
                "objectName" : "ValidationResult"
            },
            "version" : NumberLong(10014)
        }
    ]
} 

我尝试使用汇总查询,例如:

db.getCollection('ValidationResults').aggregate(
{"$match": { "_id.objectIdentifier.objectName" : "SinkPriceZonesWithFercCreditFlag", "_id.settlementInstanceId": "3b37de20-e47e-435c-896e-8d9dea0eb63a"}},
{ "$unwind": "$Versions" })

不确定如何在时间戳字段上使用$ group

1 个答案:

答案 0 :(得分:0)

要从数组中获取最后一个元素,可以使用$arrayElemAt运算符并将-1用作索引。从文档中:

  

如果为负,则$ arrayElemAt返回idx位置的元素,从数组末尾开始计数。

db.ValidationResults.aggregate(
    [
        {
            $match: { 
                "_id.objectIdentifier.objectName" : "SinkPriceZonesWithFercCreditFlag", 
                "_id.settlementInstanceId": "3b37de20-e47e-435c-896e-8d9dea0eb63a"
            }
        },
        { $unwind: "$Versions" },
        { 
            $addFields: {
                lastError: { $arrayElemAt: [ "$Versions.errors", -1 ] }
            } 
        }
])