title: $Sum function in nested document MongoDB
body: <p>I need the sum of <code>ldl_xml_count</code> for data array but I am unable to get the sum value for that field.</p>
  
  <p><strong>Design data</strong> </p>
  
  <pre><code>db.commination.findOne()
  {
      "_id" : ObjectId("5530dc5272b7e56b11497ce8"),
      "data" : [
          {
              "ldl_date" : ISODate("2015-04-08T18:30:00Z")
          },
          {
              "ldl_mmo_id" : 5
          },
          {
              "ldl_xml_count" : 1
          }
      ],
      "masterid" : [
          {
              "ldl_date" : ISODate("2015-04-08T18:30:00Z")
          },
          {
              "ldl_mmo_id" : 5
          },
          {
              "ldl_xml_count" : 1
          },
          {
              "ldl_master_info_id" : 11
          }
      ],
      "pubid" : [
          {
              "ldl_publication_info_id" : 41616,
              "ldl_xml_info_id" : 37437691
          }
      ],
      "details" : [
          {
              "ldl_id" : 54261629,
              "ldl_distribution_id" : 3289,
              "ldl_local_flag" : 1,
              "ldl_ftp_flag" : 0,
              "ldl_time" : ISODate("2015-04-09T01:06:46Z")
          }
      ]
  }
  </code></pre>
  
  <p><strong>mongo query</strong></p>
  
  <pre><code>db.commination.aggregate([
      {
          $match:{
              "data.ldl_date": {$gte: new Date("2015-01-08T18:30:00Z")}
          }
      },
      {
          $unwind :"$data"
      },
      { 
          $group: {
              _id:"$data.ldl_mmo_id", 
              total: { $sum: "$data.ldl_xml_count" } 
          } 
       }
  ])
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>{ "_id" : 1, "total" : 0 }
  
  { "_id" : 3, "total" : 0 }
  
  { "_id" : 4, "total" : 0 }
  
  { "_id" : 5, "total" : 0 }
  
  { "_id" : 2, "total" : 0 }
  
  { "_id" : null, "total" : 500000 }
  </code></pre>
  
  <p>I need the sum for id wise.</p>