title: mongodb adding up values in array based on some condition
body: <p>I can't get my head around a mongodb aggregation framework construction that adds up some values for each "_id" field documents... IF those values exist for the field "Wert".</p>
  
  <p>E.g I have a document with _id field and a conditional <code>ProduktTeilsummeDemonstrator":[]</code> or <code>"ProduktTeilsummeDemonstrator":[{Wert:342},{Wert:142}]</code> that array can be empty or not, if it is empty, I want to add a new field <code>"ProduktTeilsumme":0</code>, else, I want to add up all values in that array to the new field...</p>
  
  <p>The data that I have looks like this:</p>
  
  <pre><code>[{"_id":230,"ProduktSummeDemonstrator":713,"ProduktTeilsummeDemonstrator":[],"ProduktTeilsumme":null},{"_id":855,"ProduktSummeDemonstrator":1744,"ProduktTeilsummeDemonstrator":[],"ProduktTeilsumme":null},{"_id":767,"ProduktSummeDemonstrator":1010,"ProduktTeilsummeDemonstrator":[{"Zeitstempel":"2018-07-09T15:07:32.472Z","Wert":24},{"Zeitstempel":"2018-07-09T15:07:32.472Z","Wert":102},{"Zeitstempel":"2018-07-09T14:52:32.473Z","Wert":15},{"Zeitstempel":"2018-07-09T14:52:32.472Z","Wert":20},{"Zeitstempel":"2018-07-09T15:07:32.472Z","Wert":90},{"Zeitstempel":"2018-07-09T14:52:32.472Z","Wert":104},{"Zeitstempel":"2018-07-09T15:07:32.473Z","Wert":29},{"Zeitstempel":"2018-07-09T14:52:32.472Z","Wert":94},{"Zeitstempel":"2018-07-09T14:52:32.473Z","Wert":33},{"Zeitstempel":"2018-07-09T15:07:32.473Z","Wert":245},{"Zeitstempel":"2018-07-09T14:52:32.473Z","Wert":243},{"Zeitstempel":"2018-07-09T15:07:32.473Z","Wert":11}],"ProduktTeilsumme":null},{"_id":9,"ProduktSummeDemonstrator":94,"ProduktTeilsummeDemonstrator":[],"ProduktTeilsumme":null}]
  </code></pre>
  
  <p>I tried out different things with $reduce or $cond expressions, but somehow it won't add up: (Previously before that calculation stage I am grouping by ID and also filtering based on some time field condition..)</p>
  
  <pre><code>{
                  $project: {
                      ProduktSummeDemonstrator: "$ProduktSummeDemonstrator",
                      ProduktTeilsummeDemonstrator: {
                          $filter: {
                              input: "$res",
                              as: "res",
                              cond: { $and: [
                                  { $gte: ["$$res.Zeitstempel", new Date(req.params.start) ] },
                                  { $lte: ["$$res.Zeitstempel", new Date(req.params.end) ] }
                              ] }
                          }
                      },
                      ProduktTeilsumme:  {/*
                          $reduce: {
                              input: "$ProduktTeilsummeDemonstrator",
                              initialValue:0,
                              in: {
                                  $add: ["$$value","$$this.Wert"]
                              }
                          } */
                          $cond: { 
                              if: { $eq: [ "", "$ProduktTeilsummeDemonstrator" ] },
                              then: 0,
                              else: {
                                  $reduce: {
                                      input: "$ProduktTeilsummeDemonstrator",
                                      initialValue: 0,
                                      in: {
                                          $add: ["$$value","$$this.Wert"]
                                      }
  
                                  }
                              }
                          }                    
                      }
                  }
              }
  </code></pre>
  
  <p>at least for <code>"_id":767</code> I should get some values back, but I am getting "null" always.</p>