title: In mongodb I would like to get a count of Exit pages where it was the last page in the session based on concurrent timestamp
body: <p>I have a 'Session' collection from what I would like to get a count of Exit pages where it was the last page in the session based on concurrent timestamp.</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "_id" : "51D0DF0B0714F3F3B0A0762092BD5993_tlpns1c2b1",
      "loginId" : "-19015907",
      "pages" : {
        "consumermx" : {
          "ClientA" : {
              "activity_earnActivity" : NumberLong(1378086283000),
              "memberProfile_editAccountProfile" : NumberLong(1378086286000),
              "memberDashboard_editDashboard" : NumberLong(1378086282000)
                      }
                       }
                }
  }
  
  {
      "_id" : "E447A3356202AFFB2B67908895EFC191_tlpns1c6b16",
      "loginId" : "-22851219",
      "pages" : {
        "consumermx" : {
          "ClientA" : {
              "item_editNewSearchByCategory" : NumberLong(1378086288000),
              "memberProfile_editAccountProfile" : NumberLong(1378086345000),
              "memberProfile_editPreferencesAssociation" : NumberLong(1378086279000),
              "ssoClient_login" : NumberLong(1378086225000)
                      }
                       }
                }
  }
  </code></pre>
  
  <p>Here is what I tried but didn't work:</p>
  
  <pre class="lang-javascript prettyprint-override"><code>db.Session.aggregate ([
          {$match: {"pages.consumermx.NFCU.memberProfile_editAccountProfile": 
                       {$gte : 1378353600000, $lte : 1378440000000}}},
          {$sort: {"pages.consumermx.NFCU.memberProfile_editAccountProfile": -1}},
          {$limit: 1},
          {$group: { _id: null, count: { $sum: 1 }}}
  ])
  </code></pre>