MongoDB duplicate records in grouping

时间:2017-11-13 06:33:35

标签: mongodb

I has the following mongo query to retrieve customer survey answers:

db.getCollection('Consultant').aggregate([
{ "$match": {"Staff_ID": "NBJ42"}},
{ "$unwind": "$WeCare.CustSatisfaction" },
{ "$group": {
  "_id": {
  "country": "$Country",
  "company": "$Company_ID",
  "staff": "$Staff_ID",
  "year": "$WeCare.CustSatisfaction.Trans_Year",
  "month": "$WeCare.CustSatisfaction.Trans_Month"
},
"CustPastAnswers": {"$push": {
                    "SurveyComment": "$WeCare.CustPastAnswers.SurveyComment"
          }},
"tHH": { "$sum": "$WeCare.CustSatisfaction.HH" },
"tHN": { "$sum": "$WeCare.CustSatisfaction.HN" }
}},
{ "$group": {
  "_id": {
  "country": "$_id.country",
  "company": "$_id.company",
  "staff": "$_id.staff"
},
"Cust": {
  "$push": {
    "year": "$_id.year",
    "month": "$_id.month",
    "tHH": "$tHH",
    "tHN": "$tHN"
  }
},
"CustPastAnswers": {"$push": {
                    "SurveyComment": "$CustPastAnswers.SurveyComment"
                   }}
}},
{ "$project": { "_id": 0, "company": "$_id.company", "staff": "$_id.staff", "Cust": { "year": 1, "month": 1, "tHH": 1, "tHN": 1 }, "CustPastAnswers": 1 }}
]);

The current query gave me the following result where CustPastAnswers is duplicate and become nested array:

{
 "Cust" : [ 
     { "year" : 2017, "month" : 11, "tHH" : 11,"tHN" : 1 }, 
     { "year" : 2017, "month" : 10, "tHH" : 31, "tHN" : 3 }
 ],
 "CustPastAnswers" : [ 
     {
        "SurveyComment" : [ 
            [ 
               "very good",  "excellence tqvm nysc..."
            ]
        ]
    }, 
    {
        "SurveyComment" : [ 
            [ 
               "very good", "excellence tqvm nysc..."
            ]
        ]
    }
],
"company" : "MY01",
"staff" : "NBJ42"
}

But the result I expected for the CustPastAnswers should be something like below:

{
 "CustPastAnswers" : [ 
    {
       { "SurveyComment": "very good" },
       { "SurveyComment": "excellence tqvm nysc..." }
    }]
}

The original document is something like this:

{
   "_id" : ObjectId("5a0919fd1b4af43ca8327fda"),
   "Country" : "MY",
   "Employee_ID" : "E3058",
   "Payroll_ID" : "130942",
   "Company_ID" : "MY01",
   "Brand" : "NYSS",
   "Branch_ID" : "BJ",
   "Branch_DisplayCode" : "BJ",
   "Staff_ID" : "NBJ42",
   "Staff_Name" : "ICE WONG SHIO CHIN",
   "Staff_Alias" : "ICE WONG",
   "Resign_Date" : Date(253370649600000),
   "Job_Level_ID" : "SUP",
   "Designation" : "SUPERVISOR",
   "Disabled" : false,
   "WeCare" : {
       "CustSatisfaction" : [ 
         {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 2,
            "HH" : 6,
            "HN" : 1,
            "NH" : 1,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 3,
            "HH" : 11,
            "HN" : 0,
            "NH" : 0,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 4,
            "HH" : 10,
            "HN" : 0,
            "NH" : 1,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 5,
            "HH" : 18,
            "HN" : 0,
            "NH" : 0,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 6,
            "HH" : 12,
            "HN" : 1,
            "NH" : 0,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 7,
            "HH" : 20,
            "HN" : 1,
            "NH" : 1,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 8,
            "HH" : 19,
            "HN" : 0,
            "NH" : 1,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 9,
            "HH" : 18,
            "HN" : 0,
            "NH" : 0,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 10,
            "HH" : 31,
            "HN" : 3,
            "NH" : 0,
            "NN" : 0,
            "Disabled" : false
        }, 
        {
            "Country" : "MY",
            "Company_ID" : "MY01",
            "Brand" : "NYSS",
            "Branch_ID" : "BJ",
            "Branch_DisplayCode" : "BJ",
            "Staff_ID" : "NBJ42",
            "Payroll_ID" : "130942",
            "Trans_Year" : 2017,
            "Trans_Month" : 11,
            "HH" : 11,
            "HN" : 1,
            "NH" : 1,
            "NN" : 1,
            "Disabled" : false
        }
    ],
    "CustPastAnswers" : [ 
        {
            "SurveyAnswerId" : 907707,
            "SurveyComment" : "very good"
        }, 
        {
            "SurveyAnswerId" : 922057,
            "SurveyComment" : "excellence tqvm nysc..."
        }, 
        {
            "SurveyAnswerId" : 927697,
            "SurveyComment" : "Extraction should be done by senior beautician. Waiting time for taking off mask should be accurate and not let customer wait too long."
        }, 
        {
            "SurveyAnswerId" : 944741,
            "SurveyComment" : "Awesome tqvm nysc"
        }, 
        {
            "SurveyAnswerId" : 968338,
            "SurveyComment" : "Tqvm nysc"
        }, 
        {
            "SurveyAnswerId" : 969241,
            "SurveyComment" : "Thank for your therapist provide best ever service."
        }, 
        {
            "SurveyAnswerId" : 979755,
            "SurveyComment" : "NO. Overall the treatment is running as per normal."
        }
    ]
}
}

Any help is really appreciated.

0 个答案:

没有答案