2016-06-17 3 views
1
*I have a document structured in which we embedded the salary Document into the Employee_Detail Document.As per the MongoDb documantation ,we can use $Unwind to deconstruct the Document and use aggregated pipeline...But its not working. i am using the below script...*  
{ 
    "_id" : ObjectId("5763d4a54da83b98f269878a"), 
    "First_Name" : "fgfg", 
    "Department" : "QA", 
    "Salary" : { 
     "HRA" : "1200", 
     "Basic" : "2000", 
    } 

}) 
And i want to get sum of basic salary based on department Like 

dann Erwarteter Ausgang ist Abteilung Total_Basic ** QA 2000Summation von Teildokumentenfeld auf der Grundlage einer Gruppe unerwartetes Verhalten

I have used the following code to get the output. I have used the $unwind to  deconstruct the document.and use aggregated pipeline to group the department(Sum of basic Salary). 

db.Employee_Detail.aggregate([ 
    {$unwind:"$Salary"}, {$group: {"_id": "$Department", total_Basic: {$sum: "$Salary.Basic" } 
    }} 
])  
But i get the below Result. 
Department Total_Basic 
QA    0 

denke ich $ Abroller nicht funktioniert. Bitte schlagen Sie

Antwort

0

Ihr Hauptproblem ist der Typ des Feldes Basic ist eine Zeichenfolge. Zweitens müssen Sie die Abwicklung nicht verwenden, außer das Feld Salary enthält ein Array.

So ein Update durchführen, welche Arten von Basic und HRA zu Schwimmern (see this stackoverflow question)

zu konvertieren und dann ein Aggregat Betrieb wie dies wird Ihnen das gewünschte Ergebnis:

db.Employee_Detail.aggregate([ 
    {$group: {"_id": "$Department", total_Basic: {$sum: "$Salary.Basic" }} 
]) 
Verwandte Themen