获取 MongoDB 中具有最高值的所有数据
Getting all data with highest value in MongoDB
我在 MongoDB 5 中有一个名为“漏洞”的集合。它看起来像这样:
[
{ _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:
[
{ _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 }
]
我想只用一次查询。
有什么建议吗?
非常感谢
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"
}
}
])
解释:
- 按版本降序排序
- 按扫描器分组,从每个版本的列表中获取第一个值,并将所有值推送到测试数组中,以便我们在下一阶段适合过滤器
- 仅从测试数组中过滤我们需要的最大版本的元素
- 展开仅过滤最大值的测试数组
- 在最后的 $project 阶段,根据您需要的原始名称重命名字段
我在 MongoDB 5 中有一个名为“漏洞”的集合。它看起来像这样:
[
{ _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:
[
{ _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 }
]
我想只用一次查询。
有什么建议吗?
非常感谢
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"
}
}
])
解释:
- 按版本降序排序
- 按扫描器分组,从每个版本的列表中获取第一个值,并将所有值推送到测试数组中,以便我们在下一阶段适合过滤器
- 仅从测试数组中过滤我们需要的最大版本的元素
- 展开仅过滤最大值的测试数组
- 在最后的 $project 阶段,根据您需要的原始名称重命名字段