나는 조금 같을 것이다 SQL 무엇을 실행하여 MongoDB의 집계 프레임 워크를 사용하고 싶습니다 :몽고 집합 프레임 워크 | 여러 값으로 그룹화 하시겠습니까?
SELECT SUM(A), B, C from myTable GROUP BY B, C;
워드 프로세서 상태 :
당신은 파이프 라인의 문서에서 하나의 필드를 지정할 수 있습니다 , 이전에 계산 된 값 또는 여러 들어오는 필드로 구성된 집계 키.
그러나 실제로는 여러 수신 필드로 구성된 집계 키가 실제로 무엇인지 명확하지 않습니다.
내 데이터 세트는이 같은 비트 :
내 쿼리는 다음과 같습니다[{ "timeStamp" : 1341834988666, "label" : "sharon", "responseCode" : "200", "value" : 10, "success" : "true"},
{ "timeStamp" : 1341834988676, "label" : "paul", "responseCode" : "200", "value" : 60, "success" : "true"},
{ "timeStamp" : 1341834988686, "label" : "paul", "responseCode" : "404", "value" : 15, "success" : "true"},
{ "timeStamp" : 1341834988696, "label" : "sharon", "responseCode" : "200", "value" : 35, "success" : "false"},
{ "timeStamp" : 1341834988166, "label" : "paul", "responseCode" : "200", "value" : 40, "success" : "true"},
{ "timeStamp" : 1341834988266, "label" : "paul", "responseCode" : "404", "value" : 99, "success" : "false"}]
:
내 본능은 두 번째 그룹에 이르기까지 파이프 결과를 시도했다resultsCollection.aggregate(
{ $match : { testid : testid} },
{ $skip : alreadyRead },
{ $project : {
timeStamp : 1 ,
label : 1,
responseCode : 1 ,
value : 1,
success : 1
}},
{ $group : {
_id : "$label",
max_timeStamp : { $timeStamp : 1 },
count_responseCode : { $sum : 1 },
avg_value : { $sum : "$value" },
count_success : { $sum : 1 }
}},
{ $group : {
?
}}
);
, 내가 당신을 알고 이 작업을 수행 할 수는 있지만 첫 번째 그룹이 이미 데이터 집합을 너무 많이 축소하고 필요한 세부 수준을 잃어 버리기 때문에 작동하지 않습니다.
내가하고 싶은 것은 label
, responseCode
및 success
을 사용하는 그룹이며 그 결과의 합계를 얻습니다. 5 개 그룹이있다
label | code | success | sum_of_values | count
sharon | 200 | true | 10 | 1
sharon | 200 | false | 35 | 1
paul | 200 | true | 100 | 2
paul | 404 | true | 15 | 1
paul | 404 | false | 99 | 1
: 그것은 조금과 같아야합니다
1. { "timeStamp" : 1341834988666, "label" : "sharon", "responseCode" : "200", "value" : 10, "success" : "true"}
2. { "timeStamp" : 1341834988696, "label" : "sharon", "responseCode" : "200", "value" : 35, "success" : "false"}
3. { "timeStamp" : 1341834988676, "label" : "paul", "responseCode" : "200", "value" : 60, "success" : "true"}
{ "timeStamp" : 1341834988166, "label" : "paul", "responseCode" : "200", "value" : 40, "success" : "true"}
4. { "timeStamp" : 1341834988686, "label" : "paul", "responseCode" : "404", "value" : 15, "success" : "true"}
5. { "timeStamp" : 1341834988266, "label" : "paul", "responseCode" : "404", "value" : 99, "success" : "false"}
당신의 $ 프로젝트의 목적은 무엇인가 :
그래서, 여러 분야에 걸쳐 그룹에 당신은에서와 마찬가지로
_id : { success:'$success', responseCode:'$responseCode', label:'$label'}
를 사용할 수 있을까? 훨씬 더 큰 원본 문서입니까? 또한 건너 뛰기는 이전에 정렬 없이는별로 의미가 없습니다. avg_value가 필요한 경우 $ avg를 사용하지 않는 이유는 무엇입니까? 합계를 원하면 sum_value라고 부르지 않을까요? 또한 count_success와 count_responseCode가 같을 것이고 둘 다 필요한 이유를 모르겠습니다. –
참고 : 2.2.0-rc1부터는 여러 필드로 그룹화하는 데 약간의 변경이있었습니다. _id : {success : 1, responseCode : 1, label : 1}은 _id : {success : "$ success", responseCode : "$ responseCode", 레이블 : "$ label"}. https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/1cYch580h0w – Richard
@Richard에 따라 답변이 고정되었습니다. –