title: How to sum every fields in a sub document of MongoDB?
body: <p>I got a problem when I use db.collection.aggregate in MongoDB.</p>
  
  <p>I have a data structure like: </p>
  
  <pre><code>_id:...
  
  Segment:{
    "S1":1,
    "S2":5,
    ...
    "Sn":10
  }
  </code></pre>
  
  <p>It means the following in <code>Segment</code>: I might have several sub attributes with numeric values. I'd like to sum them up as 1 + 5 + .. + 10</p>
  
  <p>The problem is: I'm not sure about the sub attributes names since for each document the segment numbers are different. So I cannot list each segment name. I just want to use something like a for loop to sum all values together. </p>
  
  <p>I tried queries like:</p>
  
  <pre><code>db.collection.aggregate([
  
    {$group:{
      _id:"$Account",
  
      total:{$sum:"$Segment.$"}
  ])
  </code></pre>
  
  <p>but it doesn't work.</p>