I have a collection called "Vulnerabilities" in MongoDB 5. It looks like this:
[
{ _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 }
]
I would like to retrieve, for each scanner, the documents with the highest version. It should return:
[
{ _id: "...", "project": 244, "scanner": "sonarqube", "version": 2 },
{ _id: "...", "project": 244, "scanner": "sonarqube", "version": 2 },
{ _id: "...", "project": 244, "scanner": "shellcheck", "version": 3 },
{ _id: "...", "project": 244, "scanner": "powershell", "version": 4 },
{ _id: "...", "project": 244, "scanner": "powershell", "version": 4 }
]
I would like to do it with only one query.
Any suggestions?
Many thanks
db.collection.aggregate([
{
$sort: {
version: -1
}
},
{
$group: {
_id: "$scanner",
version: {
$first: "$version"
},
test: {
$push: {
v: "$version",
id: "$_id",
p: "$project"
}
}
}
},
{
$project: {
items: {
$filter: {
input: "$test",
as: "item",
cond: {
$eq: [
"$$item.v",
"$version"
]
}
}
}
}
},
{
$unwind: "$items"
},
{
$project: {
scanner: "$_id",
_id: "$items.id",
project: "$items.p",
version: "$items.v"
}
}
])
Explained: