Mongodb Collection -:
{
"_id" : ObjectId("59b0fdea8711111"),
"address" : {
"building" : "123",
},
"borough" : "Manhattan",
"grades" : [
{
"grade" : "A",
"score" : 8
},
{
"grade" : "B",
"score" : 23
},
{
"grade" : "A",
"score" : 12
},
],
"name" : "Glorious Food",
"restaurant_id" : "30112340"
}
I need to group all by grade, and then to have a list of all the names with that grade. When I run my code, i got it but I had duplicates values, as you can see than entity have 3 grades, 2 of them with A grade so that entity will appear twice in my list this is my code:
db.getCollection('restaurants').aggregate([
{$project:{
"borough":1,
"grades.grade":1,
"name":1
}},
{$match:{"borough":"Manhattan"}},
{$unwind:"$grades"},
{$group:{
"_id":"$grades",
name:{$push: {restname:"$name"}}}},
{$sort:{"_id":1}}
])
and here is an example of my output
{
"_id" : {
"grade" : "A"
},
"name" : [
{
"restname" : "Glorious Food"
},
{
"restname" : "Glorious Food"
{
"restname" : "1 East 66Th Street Kitchen"
},
bottom line, I want the restname will be distinct for each grade
Thanks!