2014-10-29 40 views
0

我想知道每個employeeid是從下面的jsonArray中的mongodb獲得的competencyTitle列表;Mongodb在jsonArray中找到

{ 
"_id" : ObjectId("54510afa1abd8deb32e82a2e"), 
"EmployeeID" : "6", 
"CompetencyTitle" : "Chemical", 
"Weightage" : 8 
} 

/* 1 */ 
{ 
"_id" : ObjectId("54510afa1abd8deb32e82a2f"), 
"EmployeeID" : "6", 
"CompetencyTitle" : "Biology", 
"Weightage" : 6 
} 

/* 2 */ 
{ 
"_id" : ObjectId("54510afa1abd8deb32e82a30"), 
"EmployeeID" : "7", 
"CompetencyTitle" : "Processes and Procedures", 
"Weightage" : 5 
} 

輸出結果如下所示;

僱員6 {化學,生物} 僱員7 {過程和程序}

回答

3

聲音基本上像一個aggregation framework問題。小輸出不同,但話又說回來,它實際上是一個有效的結構:

db.collection.aggregate([ 
    { "$group": { 
     "_id": "$EmployeeID", 
     "competencies": { "$push": "$CompetencyTitle" } 
    }} 
]) 

給出了簡單的結果:

{ 
    "_id": "6",, 
    "compentencies": [ "Chemical", "Biology" ] 
}, 
{ 
    "_id": "7", 
    "compentencies": [ "Processes and Procedures" ]   
} 
+0

看起來不錯.. THX。 – FRizal 2014-10-29 16:07:22