1

I have a collection as follows

{ "_id" : 0, "name" : "aimee Zank", "scores" : 
 [ 
   { "type" : "exam", "score" : 1.463179736705023 }, 
   { "type" : "quiz", "score" : 11.78273309957772 }, 
   { "type" : "homework", "score" : 6.676176060654615} 
 ] }

{"_id" : 1, "name" : "Aurelia Menendez", "scores" : 
[ 
 { "type" : "exam", "score" : 60.06045071030959 }, 
 { "type" : "quiz", "score" : 52.79790691903873 }, 
 { "type" : "homework", "score" : 71.761334391544 } 
] }

{"_id" : 2, "name" : "Corliss Zuk", "scores" : 
[ 
 { "type" : "exam", "score" : 67.03077096065002 }, 
 { "type" : "quiz", "score" : 6.301851677835235 }, 
 { "type" : "homework", "score" : 20.18160621941858} 
] }

Now i want the sum of all the scores of each type for respective students for example for student aimee zank i want the sum of scores for exam+quiz+homework.

I have tried this

 db.collection.aggregate(
 [
  {
   $group:
     {
       _id: "$name",
       total: { $sum: "$scores.score" },
     }
 }
 ]
 )

and this

 db.scores.aggregate(
 [
   { $project: { name: 1, total: { $add: [ "$scores.score" ] } } }
 ] 
 )

But i could not find a solution Can someone please help me with the query?

5
  • 1
    this is not a mongoDB homework i have already passed that course this is a usecase that i was trying out using that particular dataset. Commented Jan 22, 2016 at 6:15
  • 1
    Can you send me the exact question from mongoDB homework? Because i did not find any such question there and stackoverflow is a site for asking question in need, so if you know the answer you should post it otherwise there is no point in adding comments if u dont know the answer. Commented Jan 22, 2016 at 6:33
  • 1
    This is not a MongoDB homework question. The dataset is same but they have not tried solving this complex use cases in tutorial. PS: I have completed the tutorial and I am a Certified MongoDB Developer. @Ankita, Will come up with a solution soon. Commented Jan 22, 2016 at 6:49
  • 2
    @AnkitaBhowmik even if this is not MongoDB University homework, first we would like to see what have you tried so far !! Commented Jan 22, 2016 at 7:02
  • @Yogesh I have already used unwind and if u have an answer then you should probably post it , also stop adding derogatory comments such as "those who has cleared mongodb course", because even after going through the course many people have doubts when working on scenarios, you dont need to be disrespectful to beginners. Commented Jan 22, 2016 at 8:17

1 Answer 1

1

After finding no help on stackoverflow and only discouraging people in the group, i have found a solution on my own and it is just one part of the solution of i was searching for:

db.scores.aggregate(
[

 { $unwind : "$scores"},
  { $group:
     {
       _id: "$name",
       total: { $sum: "$scores.score" }
     }
 }

]
)
Sign up to request clarification or add additional context in comments.

1 Comment

Don't feel discouraged... We didn't give you solution because we knew that you're capable of solving this problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.