mongodb aggregation - unwind/group/project query combination
<p>I have records in a collection of the following format. </p>

<pre><code>//One parent record
{
    "_id" : "someDocID",
    "title" : "some title",
    "analytics" : [
            {
                    "_id" : "analyticsID1", 
                   "timeSpent" : [
                            {
                                    "time" : 14,
                                    "pageNo" : 1
                            },
                            {
                                    "time" : 4,
                                    "pageNo" : 2
                            },
                            {
                                    "time" : 3,
                                    "pageNo" : 1
                            },
                            {
                                    "time" : 1,
                                    "pageNo" : 2
                            }
                    ]                       

            },
            {                        
                    "_id" : "analyticsID2",                        
                    "timeSpent" : [
                            {
                                    "time" : 12,
                                    "pageNo" : 10
                            },
                            {
                                    "time" : 15,
                                    "pageNo" : 11
                            },
                            {
                                    "time" : 26,
                                    "pageNo" : 12
                            },
                            {
                                    "time" : 13,
                                    "pageNo" : 11
                            },
                            {
                                    "time" : 17,
                                    "pageNo" : 10
                            },
                            {
                                    "time" : 30,
                                    "pageNo" : 11
                            }
                    ]
            }
    ]               
}
</code></pre>

<p>The "pageNo" field contains repeated values. I need to group the pageNo field with adding their respective "time".</p>

<p>This is my required output. ( after "$unwind" operation on analytics )</p>

<pre><code>//Two records after "$unwind" on analytics
{
    "_id" : "someDocID",
    "title" : "some title",
    "analytics" : {
                    "_id" : "analyticsID1", 
                    "timeSpent" : [
                            {
                                    "time" : 17,   //14+3
                                    "pageNo" : 1
                            },
                            {
                                    "time" : 5,    //4+1
                                    "pageNo" : 2
                            }
                    ]
            }
}

{
    "_id" : "someDocID",
    "title" : "some title",
    "analytics" : {
                    "_id" : "analyticsID2", 
                    "timeSpent" : [
                            {
                                    "time" : 29,    //12+17
                                    "pageNo" : 10
                            },
                            {
                                    "time" : 58,    //15+13+30
                                    "pageNo" : 11
                            },
                            {
                                    "time" : 26,
                                    "pageNo" : 12
                            }                                
                    ]      
            }
}
</code></pre>

<p>I've tried various combinations of aggregate, group, unwind and project but still can't quite get there and would really appreciate any suggestions.</p>