title: Summing values across array elements
body: <p>I have the following document:</p>
  
  <pre><code>{
  "month": 2,
  "year": 2019,
  "categories": [
      {
          "title": "Income",
          "budget": 5000,
          "amount": 2000,
          "subcategories": [
              {
                  "title": "Paychecks",
                  "budget": 4000,
                  "amount": 1000,
                  "transactions": [
                      {
                          "day": 3,
                          "amount": 600,
                          "vendor": "Microsoft",
                          "memo": "Paycheck 1"
                      },
                      {
                          "day": 10,
                          "amount": 400,
                          "vendor": "Google",
                          "memo": "Paycheck 2"
                      }
                  ]
              },
              {
                  "title": "Other",
                  "budget": 1000,
                  "amount": 1000,
                  "transactions": [
                      {
                          "day": 7,
                          "amount": 600,
                          "vendor": "Mom",
                          "memo": "Gift for Baby"
                      },
                      {
                          "day": 19,
                          "amount": 400,
                          "vendor": "Amy",
                          "memo": "Gift for Julie"
                      }
                  ]
              }
          ]
      },
      {
          "title": "Housing",
          "budget": 3000,
          "amount": 3000,
          "subcategories": [
              {
                  "title": "Rent",
                  "budget": 2000,
                  "amount": 2000,
                  "transactions": [
                      {
                          "day": 1,
                          "amount": 2000,
                          "vendor": "Homes4Rent",
                          "memo": "Rent"
                      }
                  ]
              },
              {
                  "title": "Electric",
                  "budget": 1000,
                  "amount": 1000,
                  "transactions": [
                      {
                          "day": 14,
                          "amount": 1000,
                          "vendor": "Tampa Electric",
                          "memo": "Electric"
                      }
                  ]
              }
          ]
      }
  ]}
  </code></pre>
  
  <p>I need to ensure that the "categories.budget" amount is always equal to the sum of the "categories.subcategories.budget" amounts for that category.</p>
  
  <p>For example, I need to ensure that the budget for "Income" is 5000 because the sum of the budgets for its subcategories ("Paychecks" and "Other") is exactly 5000.</p>
  
  <p>The most desirable solution would be one that can perform the sum and the "categories.budget" field update in a single query but I'm unsure how to accomplish this or if this is even possible. Here's what I've come up with so far:</p>
  
  <pre><code>db.collection.aggregate(
  [
      {
          $match: {
              month:2,
              year:2019
          }
      },
      {
          $unwind: "$categories"
      },
      {
          $unwind: "$categories.subcategories"
      },
      {
          $group: {
              _id: {
                  category: "$categories.title"
              },
              sum: {
                  $sum: "$categories.subcategories.budget"
              }
          }
      }
  ])
  </code></pre>
  
  <p>Unfortunately, this only seems to be printing out the category budgets instead of summing them. Whichever solution is realized will ultimately be used in various places throughout my application to perform similar operations as well.</p>
  
  <p>Thank you so much and let me know if I need to provide any further information.</p>