0

I have the below mongo collection structure

    {
   "_id":"some mongo Id",
   "email":"[email protected]",
   "Course1":[
      {
         "sublesson":1,
         "timespent":10,
         "timestamp":"123456"
      },
      {
         "sublesson":1,
         "timespent":20,
         "timestamp":"123457"
      },
      {
         "sublesson":2,
         "timespent":20,
         "timestamp":"123458"
      }
   ]
}

Now, all need is to find the sum of time taken for each sublesson aggregated. I have tried some methods but cannot access the data inside the "course1" array.

0

1 Answer 1

1

If I can understand the requirement correctly you need to sum up the timespent.

Please give this a try:

db.testing.aggregate(
[
    {
        "$unwind":"$Course1"
    },
    {
        "$group": {
            "_id": "$email", 
            "timespent":
            {
                "$sum":"$Course1.timespent"
            }
        }
    }
]
);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.