1

나는 "Feedback" 컬렉션에 저장된 데이터를 가진 NodeJS/MongoDB 앱을 가지고 있습니다. 데이터는 다음과 같습니다.몽구스 : 집계 및 집계

[ 
    { 
     "__v": 0, 
     "_id": "57d6b2d09f46ca14440ac14e", 
     "customerFeedback": [ 
      { 
       "_id": "57d6b2d09f46ca14440ac14f", 
       "answer": [ 
        { 
         "_id": "57d6b2d09f46ca14440ac150", 
         "answerValue": "cat", 
         "answerWeight": 0 
        } 
       ], 
       "question": "What is your favourite thing about this shop?", 
       "questionId": "57d65edc0132461120fa0afd" 
      }, 
      { 
       "_id": "57d6b2d09f46ca14440ac151", 
       "answer": [ 
        { 
         "_id": "57d6b2d09f46ca14440ac152", 
         "answerValue": "Okay", 
         "answerWeight": 0 
        } 
       ], 
       "question": "How was your experience today?", 
       "questionId": "57d69ef6dbb25611e46e6bc9" 
      } 
     ], 
     "shopId": "SH0001", 
     "feedbackCreatedOn": "2016-09-12T13:51:12.703Z", 
     "questionsForDay": "2016-09-12T00:00:00Z" 
    }, 
    { 
     "__v": 0, 
     "_id": "57d6b3389f46ca14440ac157", 
     "customerFeedback": [ 
      { 
       "_id": "57d6b3389f46ca14440ac158", 
       "answer": [ 
        { 
         "_id": "57d6b3389f46ca14440ac159", 
         "answerValue": "cat", 
         "answerWeight": 0 
        } 
       ], 
       "question": "What is your favourite thing about this shop?", 
       "questionId": "57d65edc0132461120fa0afd" 
      }, 
      { 
       "_id": "57d6b3389f46ca14440ac15a", 
       "answer": [ 
        { 
         "_id": "57d6b3389f46ca14440ac15b", 
         "answerValue": "Very Good", 
         "answerWeight": 0 
        } 
       ], 
       "question": "How was your experience today?", 
       "questionId": "57d69ef6dbb25611e46e6bc9" 
      }, 
      { 
       "_id": "57d6b3389f46ca14440ac15c", 
       "answer": [ 
        { 
         "_id": "57d6b3389f46ca14440ac15d", 
         "answerValue": "Cost", 
         "answerWeight": 0 
        } 
       ], 
       "question": "What would you like us to improve on?", 
       "questionId": "57d6b32d9f46ca14440ac153" 
      } 
     ], 
     "shopId": "SH0001", 
     "feedbackCreatedOn": "2016-09-12T13:52:56.939Z", 
     "questionsForDay": "2016-09-12T00:00:00Z" 
    }, 
    { 
     "__v": 0, 
     "_id": "57d6c8eb97157f10a4e5c2e7", 
     "customerFeedback": [ 
      { 
       "_id": "57d6c8eb97157f10a4e5c2e8", 
       "answer": [ 
        { 
         "_id": "57d6c8eb97157f10a4e5c2ea", 
         "answerValue": "Customer Experience", 
         "answerWeight": 0 
        }, 
        { 
         "_id": "57d6c8eb97157f10a4e5c2e9", 
         "answerValue": "Others", 
         "answerWeight": 0 
        } 
       ], 
       "question": "What would you like us to improve on?", 
       "questionId": "57d6b7d99ee61e47f01e5334" 
      } 
     ], 
     "shopId": "SH0003", 
     "feedbackCreatedOn": "2016-09-12T15:25:31.724Z", 
     "questionsForDay": "2016-09-12T00:00:00Z" 
    } 
] 

결과 배열에는 이러한 항목이 많이 있지만 위의 데이터는이를 설명하는 데 사용됩니다.

제 질문은 주어진 shopIdquestionId에 대해 각 개인이 answerValue 번 발생하는 횟수를 계산하려고합니다. 어떻게해야합니까?

나는

db.Feedback.find({shopId:"SH0001",'customerFeedback.questionId':"57d65edc0132461120fa0afd"}) 

(예를 들어)로 찾기 방법을 사용하여 필요한 데이터 세트까지 결과를 필터링 할 수 있어요하지만 내가 원하는 형식으로 데이터를 집계하는 방법을 모르겠어요.

답변

2

이 파이프 라인은 당신에게 당신이 경우 추가 $match 단계 아래로 결과를 필터링 할 수 있습니다 샘플 데이터 물론 다음과 같은 출력

/* 1 */ 
{ 
    "_id" : { 
     "shopId" : "SH0003", 
     "questionId" : "57d6b7d99ee61e47f01e5334", 
     "answerValue" : "Others" 
    }, 
    "count" : 1.0 
} 

/* 2 */ 
{ 
    "_id" : { 
     "shopId" : "SH0003", 
     "questionId" : "57d6b7d99ee61e47f01e5334", 
     "answerValue" : "Customer Experience" 
    }, 
    "count" : 1.0 
} 

/* 3 */ 
{ 
    "_id" : { 
     "shopId" : "SH0001", 
     "questionId" : "57d65edc0132461120fa0afd", 
     "answerValue" : "cat" 
    }, 
    "count" : 2.0 
} 

/* 4 */ 
{ 
    "_id" : { 
     "shopId" : "SH0001", 
     "questionId" : "57d69ef6dbb25611e46e6bc9", 
     "answerValue" : "Okay" 
    }, 
    "count" : 1.0 
} 

/* 5 */ 
{ 
    "_id" : { 
     "shopId" : "SH0001", 
     "questionId" : "57d6b32d9f46ca14440ac153", 
     "answerValue" : "Cost" 
    }, 
    "count" : 1.0 
} 

/* 6 */ 
{ 
    "_id" : { 
     "shopId" : "SH0001", 
     "questionId" : "57d69ef6dbb25611e46e6bc9", 
     "answerValue" : "Very Good" 
    }, 
    "count" : 1.0 
} 

을 제공

db.getCollection("yourCollection").aggregate([ 
    { $unwind: "$customerFeedback" }, 
    { $unwind: "$customerFeedback.answer" }, 
    { 
     $group: { 
      _id: { 
       shopId: "$shopId", 
       questionId: "$customerFeedback.questionId", 
       answerValue: "$customerFeedback.answer.answerValue" 
      }, 
      count: { $sum: 1 } 
     } 
    } 
]) 

원하는 결과를 제공해야 특정 값에만 관심이있다.


위로 마감일은 언급합니다 : 당신이 그렇게

... 
{ $unwind: "$customerFeedback" }, 
{ $match: { shopId: "SH0001", "customerFeedback.questionId": "57d65edc0132461120fa0afd" } }, 
{ $unwind: "$customerFeedback.answer" }, 
{ 
    $group: { 
     _id: { 
      shopId: "$shopId", 
      questionId: "$customerFeedback.questionId", 
      answerValue: "$customerFeedback.answer.answerValue" 
     }, 
     count: { $sum: 1 } 
    } 
} 
... 

어떤

{ 
    "_id" : { 
     "shopId" : "SH0001", 
     "questionId" : "57d65edc0132461120fa0afd", 
     "answerValue" : "cat" 
    }, 
    "count" : 2.0 
} 

초래처럼 고객 피드백의 중첩 된 배열을 풀기 후 사용할 수 $match와 아래로

당신의 결과를 필터링하려면하면 많은 수의 문서 또는 인덱스가 shopId 및/또는 customerFeedback.questionId 인 경우 $match 스테이지를 파이프 라인의 전면에 복제하여 t 문서 만 풀 수 있습니다. 그는 해당 상점과 원하는 질문에 대한 적어도 하나의 피드백을받습니다. 그래서 (정확한 관점에서 옵션 최적화) 그럴 것 같습니다.

... 
{ $match: { shopId: "SH0001", "customerFeedback.questionId": "57d65edc0132461120fa0afd" } }, 
{ $unwind: "$customerFeedback" }, 
{ $match: { shopId: "SH0001", "customerFeedback.questionId": "57d65edc0132461120fa0afd" } }, 
{ $unwind: "$customerFeedback.answer" }, 
{ 
    $group: { 
     _id: { 
      shopId: "$shopId", 
      questionId: "$customerFeedback.questionId", 
      answerValue: "$customerFeedback.answer.answerValue" 
     }, 
     count: { $sum: 1 } 
    } 
} 
... 
+0

안녕하세요! 답장을 보내 주셔서 감사합니다. QuestionId로 결과를 필터링하고 싶습니다. 파이프 라인에서 $ match 절을 어디에 추가해야합니까? 나는 그 일을 시도했지만, 나는 그다지 성공하지 못했습니다. questionId는 CustomerFeedback 내부에 중첩되어 있으며 저에게 충격을줍니다. –

+0

내 업데이트보기 - 도움이 되시길 바랍니다. – DAXaholic

+0

답장을 보내 주셔서 감사합니다. DB 셸에서 작업 할 수 있습니다. 그러나 난 내 애플 리케이션에서 그것을 사용하여 어려움에 직면하고있어. 설명 할 질문을 업데이트했습니다. –