title: Count entries and store as field
body: <p>I have the following structure in my DB and I need to count "Tripduration" for all the childs and save it under "Tripcount", since I have an entry for the duration for every trip:</p>
  
  <pre><code>{
      "_id": 1,
      "Tripcount": "This should be 5",
      "Trips": [
          {
              "EndStationID": 2,
              "Tripduration": [
                  43,
                  50,
                  45
              ]
          },
          {
              "EndStationID": 3,
              "Tripduration": [
                  52
              ]
          },
          {
              "EndStationID": 4,
              "Tripduration": [
                  34
              ]
          }
  </code></pre>
  
  <p>I have the following query, however it always returns 0. I am new to the MongoDB syntax and was hoping that someone sees why it returns 0:</p>
  
  <pre><code>db.test.aggregate( 
    { 
      $group: { 
        _id:   {"StationID": "$Start Station ID", "EndStationID": "$End Station ID"}, 
        durations: {$addToSet: "$Trip Duration" },
      } 
    }, 
    { 
      $group: {
        _id: "$_id.StationID",
        Tripcount: {$sum: "durations"},
        Trips: { $addToSet: { EndStationID: "$_id.EndStationID", Tripduration: "$durations"} } 
      } 
    } 
  )
  </code></pre>