Mar*_*cel 2 mongodb mongodb-query aggregation-framework
在我的aggreagate管道中执行展开后,我有中间结果,例如:
[
{_id:1, precision:0.91, recall:0.71, other fields...},
{_id:1, precision:0.71, recall:0.81, other fields...},
{_id:1, precision:0.61, recall:0.91, other fields...},
{_id:2, precision:0.82, recall:0.42, other fields...},
{_id:2, precision:0.72, recall:0.52, other fields...},
{_id:2, precision:0.62, recall:0.62, other fields...}
]
Run Code Online (Sandbox Code Playgroud)
现在我想通过_id对文档进行分组,然后在每个组中查找最大召回的文档,并获取此文档的召回,精度和_id.
结果将是:
[
{_id:1, precisionOfDocWithMaxRecall:0.61, maxRecall:0.91},
{_id:2, precisionOfDocWithMaxRecall:0.62, maxRecall:0.62}
]
Run Code Online (Sandbox Code Playgroud)
我已设法使用group和max但没有精度字段获得结果.
您可以运行以下管道,它使用$sort运算符来命令文档$group首先进入管道,然后使用$first(或$last,根据排序方向)返回有序列表中的第一个/最后一个元素:
db.collection.aggregate([
/* previous pipeline */
{ "$sort": { "recall": -1 } },
{
"$group": {
"_id": "$_id",
"precisionOfDocWithMaxRecall": { "$first": "$precision" },
"maxRecall": { "$first": "$recall" }
}
}
])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1415 次 |
| 最近记录: |