FRizal
FRizal

Reputation: 448

Mongodb find in jsonArray

I would like to know for each employeeid is their list of competencyTitle from the mongodb in jsonArray below ;

{
"_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
 }

The output to be something like below;

EmployeeID 6 { Chemical,Biology} EmployeeID 7 {Processes and Procedures}

Upvotes: 0

Views: 71

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151132

Sound basically like an aggregation framework problem. A little different in output but then again it's actually a valid structure:

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

Gives simple results:

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

Upvotes: 3

Related Questions