【发布时间】: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