【问题标题】:Mongo Db Aggregation with $project and $max带有 $project 和 $max 的 Mongodb 聚合
【发布时间】:2018-07-16 17:26:20
【问题描述】:

以下是我的示例文档:我想使用聚合查询获取最大版本的错误。

{
    "_id": {
      "objectIdentifier": {
        "identifier": {
          "Empid": "715e66c7-92ff-4619-9324-2c708489fe27"
        },
        "objectName": "EmployeeInfoValidationValue"
      },
      "Empid": "715e66c7-92ff-4619-9324-2c708489fe27"
    },
    "Versions": [
      {
        "errors": [
          {
            "level": "ERROR",
            "message": "Employee Name missing",
            "timestamp": NumberLong(1531509214154)
          }
        ],
        "resultSetId": "6314a9b1-1bb0-4ba7-8128-9db39085339c",
        "scope": "INPUT_QUALITY",
        "validationLevelSeverity": "ERROR",
        "validationResultsIdentifier": {
          "identifier": {
            "objectIdentifier": {
              "identifier": {
                "Empid": "715e66c7-92ff-4619-9324-2c708489fe27"
              },
              "objectName": "EmployeeInfoValidationValue"
            },
            "Empid": "715e66c7-92ff-4619-9324-2c708489fe27"
          },
          "objectName": "ValidationResult"
        },
        "version": NumberLong(10039)
      },
      {
        "errors": [
          {
            "level": "ERROR",
            "message": "Employee Name length is not within specified range",
            "timestamp": NumberLong(1531123789457)
          }
        ],
        "resultSetId": "8743100a-1464-46af-b4d6-6636c47c8f36",
        "scope": "INPUT_QUALITY",
        "validationLevelSeverity": null,
        "validationResultsIdentifier": {
          "identifier": {
            "objectIdentifier": {
              "identifier": {
                "Empid": "715e66c7-92ff-4619-9324-2c708489fe27"
              },
              "objectName": "EmployeeInfoValidationValue"
            },
            "Empid": "715e66c7-92ff-4619-9324-2c708489fe27"
          },
          "objectName": "ValidationResult"
        },
        "version": NumberLong(10097)
      }
    ]
  }

我已经编写了聚合查询,它显示了所有具有最大版本的记录 - 例如显示最大版本:NumberLong(10097)。在这种情况下,我不确定如何将Versions.errors 显示为“员工姓名长度不在指定范围内”。 这是我写的查询:

db.ValidationResults.aggregate(
    [
            {
            $match: { 
                "_id.Empid" : "715e66c7-92ff-4619-9324-2c708489fe27",
                "Versions.scope" : "INPUT_QUALITY"
            }
        },

        {
          $project:
            {
                versionMax: {$max: "$Versions.version"}
     }
 }
 ]
     ) 

我尝试$addFields 显示错误,但它只返回 null(不工作)。 TIA

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    您可以在下一个管道阶段使用versionMax$filter 来获得version 等于$max 值的单项数组。然后你可以使用$unwind 将一个元素的数组转换为一个对象:

    db.ValidationResults.aggregate([
        {
            $project: {
                Versions: 1,
                MaxVersion: { $max: "$Versions.version" }
            }
        },
        {
            $project: {
                Versions: {
                    $filter: {
                        input: "$Versions",
                        as: "x",
                        cond: { $eq: [ "$$x.version", "$MaxVersion" ] }
                    }
                }
            }
        },
        {
            $unwind: "$Versions"
        }
    ])
    

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 2012-10-24
      • 2015-12-03
      • 2015-07-04
      • 2018-03-27
      • 2021-04-11
      • 2018-05-09
      • 2019-12-26
      相关资源
      最近更新 更多