2016-08-12 2 views
1

の最大値を持つ文書を見つけ、私は中間結果を持っているような:MongoDBは私のaggreagateパイプラインでリラックス実行した後に特定のフィールド(ARGMAX)

[ 
{_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でグループ文書をしたいと思い、各グループで、最大のリコールを持つ文書を見つけ、この文書のリコール、precison、および_idを取得します。

だから、結果は次のようになります。

[ 
    {_id:1, precisionOfDocWithMaxRecall:0.61, maxRecall:0.91}, 
    {_id:2, precisionOfDocWithMaxRecall:0.62, maxRecall:0.62} 
] 

私はグループを使用して結果を得るために管理し、最大なく、精度のフィールドなしています。

答えて

2

あなたは、次のパイプラインを実行することができますが、それは最初$groupパイプラインになった文書を注文する$sort演算子を使用して、使用しています$first(またはソート方向に応じ$last、)へ順序付きリストの最初/最後の要素を返します。

db.collection.aggregate([ 
    /* previous pipeline */ 
    { "$sort": { "recall": -1 } }, 
    { 
     "$group": { 
      "_id": "$_id", 
      "precisionOfDocWithMaxRecall": { "$first": "$precision" }, 
      "maxRecall": { "$first": "$recall" } 
     } 
    } 
]) 
関連する問題