2017-12-29 5 views
-2

집계 프레임 워크를 사용하고 있습니다. $match을 사용하는 동안 일치하는 항목이 없으면 $ group에 개수를 가져오고 싶습니다. 하지만 0 요소 가져 오기가 올 것입니다.

쿼리는 다음과 같습니다.

[{"$project":{ 
    "totalOffered":"$totalOffered", 
    "totalOpen":"$totalOpen", 
    "totalJoined":"$totalJoined", 
    "totalInter":"$totalInter", 
    "reportData":{"$arrayElemAt":["$repo",0]}, 
    "repo":{"$arrayElemAt":["$repo",0]} 
}}, 
{"$unwind":"$reportData"}, 
{"$unwind":"$reportData.candidates"}, 
{"$unwind":"$reportData.candidates.candidateStatus"}, 
{"$match":{"reportData.candidates.candidateStatus.status":"Joined"}}, 
{"$group":{"_id":null,"totalCount":{"$sum":1}}}] 

일치하는 항목이 없으면 카운트를 0으로 만드는 방법은 무엇입니까?

+0

뭘 하려는지 명확하지 않습니다. 파이프 라인은 일치가 발견되지 않으면 연속적으로 사용될 때 데이터를 서로 전달하고 그룹화 할 대상은 무엇입니까? 질문을 수정하고 더 나은 예제를 게시해야 할 수도 있습니다. – Avij

+0

구체적으로 집계에 대해 묻고 있습니까? count 명령으로이 작업을 수행 할 수 있으며 일치하는 항목이 없을 때 0을 반환합니다. 'db.coll.count ({query-here})'는 0을 반환하거나 많은 문서가 조건을 만족시킵니다. –

+0

{$ project : {totalOffered : "$ totalOffered", totalOpen : "$ totalOpen", totalJoined : "totalJoined", totalInter : "$ totalInter", "reportData": {$ arrayElemAt : [ "$ repo", 0] {$ unwind : "$ reportData.candidates"}, {$ unwind : "$ reportData.candidates"}, {$ unwind : "$ reportData.candidates"}, "repo": {$ arrayElemAt : [ "$ repo", 0]}}} $ reportData.candidates.candidateStatus "}, {$ match : {"reportData.candidates.candidateStatus.status ":"Joined "}}, {$ group : {_id : null, totalCount : {$ sum : 1}}} 여기서 'Joined'에 대한 $ match가 0 결과를 가져 오면 $ project (totalOpen, totalOffered)의 이전 데이터가 사라집니다. 이것을 극복하는 방법? –

답변

0

마지막 및 $group$group으로 바꿀 수 있습니다.

가 일치하면 group accumulators의 조합을 사용할 필요가 이전 단계에서 데이터를 유지하기 위해 다른 0

{"$group":{ 
    "_id":null, 
    "totalCount":{ 
    "$sum":{ 
     "$cond":[ 
     {"$eq":["$reportData.candidates.candidateStatus.status","Joined"]}, 
     1, 
     0 
     ] 
    } 
    } 
}} 

출력 1을 찾을 수 있도록 할 때 조건 연산자에 일치하는 논리를 이동했다.

관련 문제