2016-06-01 4 views
1

私は、突起部を次のようしている。フィルタアレイおよび特定のプロパティを返し

db.tickets.aggregate([ 
{ 
    $match: { 
    "satisfaction_rating.id": {"$exists": true} 
    } 
}, 
{ 
    $project: { 
    "ticketId": "$id", 
    "employee": "$assignee.name", 
    "subject": "$subject", 
    "memberId": { 
     "$filter": { 
     "input": "$custom_fields", 
     "as": "field", 
     "cond": {"$eq": ["$$field.id", 24685851]} 
     } 
    }, 
    "requester": "$requester.name", 
    "created": "$created_at", 
    "solved": "$metric_set.solved_at", 
    "resolutionTimeInHours": {"$trunc": {"$divide": ["$metric_set.full_resolution_time_in_minutes.business", 60]}}, 
    "score": "$satisfaction_rating.score", 
    "comment": "$satisfaction_rating.comment" 
    } 
}, 
{ 
    $out: "satisfaction" 
}]); 

$フィルターは配列を返します。私がしたいのは、最初の要素を取得し、1つの要素を持つ配列の代わりにmemberIdにバインドされるプロパティを取得することです。

私はしばらく見ていましたが、適切な解決策を見つけられませんでした。 誰も私にヒントを与えることができますか?

答えて

1

@ user3100115正しい方向に私を導いた。

ここに私の最終的な解決策:一歩

"memberId": { 
    "$let": { 
     "vars": { 
      "memberId": { 
       "$arrayElemAt": [ 
       {"$filter": { 
         "input": "$custom_fields", 
         "as": "field", 
         "cond": {"$eq": ["$$field.id", 24685851]} 
       }} 
       ,0] 
       } 
     }, 
     "in": "$$memberId.value" 
    } 
} 
1

あなたは$filterの結果から要素を返し、サブ文書のフィールドにアクセスするためにdot notation$let演算子を使用するように$arrayElemAt演算子を使用することができます。

"memberId": { 
    "$let": { 
     "vars": { 
      "field": { 
       "$arrayElemAt": [ 
        "$filter": { 
         "input": "$custom_fields", 
         "as": "field", 
         "cond": { "$eq": [ "$$field.id", 24685851 ]} 
        }, 
        0 
       ] 
      } 
     }, 
     "in": "$$field.id" 
    } 
} 
+0

。しかし、どのようにして、要素そのものではなく最初の要素のプロパティを取得できますか? –

+0

'id'フィールドだけが必要ですか? – styvane

関連する問題