每个组的Mongodb查找特定字段(argmax)值最大的文档
在我的聚合管道中执行解卷后,我得到了中间结果,例如:每个组的Mongodb查找特定字段(argmax)值最大的文档,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,在我的聚合管道中执行解卷后,我得到了中间结果,例如: [ {_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: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...}
]
现在我想按_id对文档进行分组,然后在每个组中找到召回率最高的文档,并获得该文档的召回率、准确度和_id
因此,结果将是:
[
{_id:1, precisionOfDocWithMaxRecall:0.61, maxRecall:0.91},
{_id:2, precisionOfDocWithMaxRecall:0.62, maxRecall:0.62}
]
我使用group和max获得了结果,但没有精度字段。您可以运行以下管道,它使用操作符对进入管道的文档进行排序,然后使用(或,具体取决于排序方向)要返回有序列表中的第一个/最后一个元素,请执行以下操作:
db.collection.aggregate([
/* previous pipeline */
{ "$sort": { "recall": -1 } },
{
"$group": {
"_id": "$_id",
"precisionOfDocWithMaxRecall": { "$first": "$precision" },
"maxRecall": { "$first": "$recall" }
}
}
])