2012-10-03 5 views
2

이 질문은 두 부분으로 구성되어 있습니다. 컬렉션 구조는 다음과 같습니다MongoDB PostgreSQL에 해당하는 집계 쿼리

_id : MongoID,
AGENT_ID : 문자열,
결과 : 문자열,
created_on : ISO 날짜,
... 다른 분야 ...

1 부 :
원하는 출력 : PostgreSQL을 사용하는 동등한 SQL을 사용하여 count : TUPLE 표현으로 각 agent_id 및 결과 조합에 대해 하나의 결과.

("1234", "Success", 4), 
("1234", "Failure", 4), 
("4567", "Success", 3), 
("7896", "Failure", 2), 
..... 

SELECT agent_id, result, count(*) 
FROM table 
GROUP BY agent_id, result 
HAVING created_on >= now()::date; 

나는 아래의 mongo 쿼리를 생각해 냈습니다 ... 나는 개념적 또는 구문 오류가 있다고 생각합니다. 문서에서는 $ match early in the pipeline:을 사용한다고하지만, $ match를 사용하여 쿼리를 실행하면 쿼리가 제한되지만 $ 그룹을 추가하자마자 많은 결과가 나타납니다. 또한 하나 이상의 필드로 그룹화하는 방법을 이해할 수없는 것 같습니다. 위의 SQL 쿼리와 같은 결과를 얻기 위해 아래의 쿼리를 어떻게 편집 할 수 있습니까?

db.collection.aggregate(
    { $match : 
    { created_on: 
     { $gte: new Date('08-13-2012') //some arbitrary date 
    } 
    }, $group: 
    { _id:"$agent_id" }, 
    $project: 
    {_id:0, agent_id:1, result:1} 
}) 

파트 2) 첫 번째 결과 집합은 적절하지만, 최적이 아닌 것이다. PostgreSQL의 I와 같은 결과 집합을 얻을 수

("1234", { "Success", "Failure" }, { 4, 3 }), 
("4567", { "Success", "Failure" }, { 3, 0 }), 
("7896", { "Success", "Failure" }, { 0, 2 }) 

I 배열 데이터 타입 및 set_to_array 기능 (custom 함수)와 PostgreSQL을에이를 수있다. 대학원 특정 SQL은 다음과 같습니다

[ 
    { "1234", [ { "success": 4 }, { "failure": 4 } ] }, 
    { "4567", [ { "success": 3 }, { "failure": 0 } ] }, 
    { "7896", [ { "success": 0 }, { "failure": 0 } ] } 
] 

가 MongoDB를 집계 프레임 워크이 원하는 압축 결과를 얻을 수 있나요 :

SELECT agent_id, set_to_array(result), set_to_array(count(*)) 
FROM table 
GROUP BY agent_id, result 
HAVING created_on >= now()::date; 

내가 MongoDB의에 해당하는 데이터 구조의 모습 생각? 여기

답변

3

당신은 갈 :

만든 테스트 데이터 :

db.test.insert ({AGENT_ID : "1234"결과 "실패", created_on : 새 Date()}) ; db.test.insert ({agent_id : "1234", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1234", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1234", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1234", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1234", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1234", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "성공", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); db.test.삽입 ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); db.test.insert ({agent_id : "1324", 결과 : "실패", created_on : 새 날짜()}); 2 부 추가

{ 
"result" : [ 
    { 
     "count" : 7, 
     "agent_id" : "1324", 
     "result" : "Failure" 
    }, 
    { 
     "count" : 4, 
     "agent_id" : "1324", 
     "result" : "Success" 
    }, 
    { 
     "count" : 4, 
     "agent_id" : "1234", 
     "result" : "Success" 
    }, 
    { 
     "count" : 3, 
     "agent_id" : "1234", 
     "result" : "Failure" 
    } 
], 
"ok" : 1 
} 

- 1 부 꽤 비슷한하지만 계산은 좀 더 복잡하다;

db.test.aggregate(
    { 
    $match:{ /* filter out the things you want to aggregate */ 
     created_on:{$gte:new Date(1000000)} 
    } 
    }, 
    { 
    $group: {_ 
     _id: { /* the things you want to group on go in the _id */ 
     agent_id:"$agent_id", 
     result:"$result" 
     }, 
     count:{$sum:1} /* simple count */ 
    } 
    }, 
    { 
    $project: { /* take the id out into the separate fields for your tuple. */ 
     _id:0, 
     agent_id:"$_id.agent_id", 
     result:"$_id.result", 
     count:"$count" 
    } 
    }); 

을 제공합니다

db.test.aggregate(
    { 
    $match: { 
     created_on: {$gte:new Date(1000000)} 
    } 
    }, 
    { 
    $group: { 
     _id: { 
     agent_id:"$agent_id" 
     }, 
     failure: { 
     $sum:{ 
      $cond:[ 
      {$eq:["$result","Failure"]}, 
      1, 
      0 
      ] 
     } 
     }, 
     success: { 
     $sum: { 
      $cond:[ 
      {$eq:["$result","Success"]}, 
      1, 
      0 
      ] 
     } 
     } 
    } 
    }, 
    { 
    $project: { 
     _id: 0, 
     agent_id: "$_id.agent_id", 
     failure: "$failure", 
     success: "$success" 
    } 
    }); 

가 제공합니다 :

{ 
"result" : [ 
    { 
     "failure" : 7, 
     "success" : 4, 
     "agent_id" : "1324" 
    }, 
    { 
     "failure" : 3, 
     "success" : 4, 
     "agent_id" : "1234" 
    } 
], 
"ok" : 1 
} 
+0

는 "총계"를 얻기 위해 다른 즐거움과 휴식을 체험 할 것이 가능 기본적으로 당신은 당신이 계산 원하는 것을 일치하는 경우에만 계산? 나는 각 결과 (실패와 성공의 합)에 부분합을 추가 할 수 있었지만, 모든 에이전트 성공과 실패를 합산하는 레코드를 생성하는 것이 질의 자체에서 가능한지 확실하지 않습니다. – Ketema

+0

같은 검색어 또는 새 검색어를 의미합니까? –

+0

결과가 동일한 쿼리에서 총합계를 가질 수 있다면 흥미로울 것입니다. – Ketema