MongoDB 按组中检索到的值过滤

MongoDB filter by retrieved value in group

我在 MongoDB 中有这个 collection:

[
  { _id: "...", "project": 244, "scanner": "sonarqube", "version": 1 },
  { _id: "...", "project": 244, "scanner": "sonarqube", "version": 2 },
  { _id: "...", "project": 244, "scanner": "sonarqube", "version": 2 },
  { _id: "...", "project": 244, "scanner": "shellcheck", "version": 1 },
  { _id: "...", "project": 244, "scanner": "shellcheck", "version": 2 },
  { _id: "...", "project": 244, "scanner": "shellcheck", "version": 3 },
  { _id: "...", "project": 244, "scanner": "powershell", "version": 2 },
  { _id: "...", "project": 244, "scanner": "powershell", "version": 3 },
  { _id: "...", "project": 244, "scanner": "powershell", "version": 4 },
  { _id: "...", "project": 244, "scanner": "powershell", "version": 4 }
]

我想要 return 项目数,按扫描仪分组,最新(最高)版本:

{ scanner: "sonarqube", count: 2 }, // All items with sonarqube and version: 2
{ scanner: "shellcheck", count: 1 }, // All items with shellcheck and version: 3
{ scanner: "powershell", count: 2 } // All items with powershell and version: 4

到目前为止,我得出了这个:

db.getCollection("vulnerabilities").aggregate([
{
    $match: { project_id: 422 }
},
{
    $sort: { version: -1 }
},
{
    $group: {
        _id: "$scanner",
        'count': { $first: "$version"},
    }
}
])

这是每个组的 return 最新(最高)版本:

sonarqube  | 2 |
shellcheck | 3 |
powershell | 4 |

现在,我要告诉Mongo:

有什么想法、建议吗?

谢谢

最简单的方法是先获取计数,然后再按版本排序,也许:

db.getCollection("vulnerabilities").aggregate([
{
    $match: { project_id: 422 }
},
{   
    $group:{
       _id: { scanner: "$scanner", version: "$version"},
       count: { $sum: 1 }
    }
},
{
    $sort: { "_id.version": -1 }
},
{
    $group: {
        _id: "$_id.scanner",
        count: { $first: "$count" },
        version: { $first: "$_id.version" }
    }
}
])

$group阶段可以使用$max

db.test.aggregate([ 
    {$group : {_id : "$scanner", count : {$sum : 1}, version : {$max : "$version"}}}
])

或者您可以 $sort 按版本并使用 $first

db.test.aggregate([
    {$sort : {version: -1}}, 
    {$group : {_id : "$scanner", count : {$sum : 1}, version : {$first : "$version"}}}
])