title: Inserting/Updating Aggregated data on Mongo
body: <p>Consider the following collection:</p>
  
  <p><strong>requests</strong>-></p>
  
  <pre><code>{
    _id : ObjectId("573f28f49b0ffc283676f736"),
    date : '2018-12-31',
    userId: ObjectId("5c1239e93a7b9a72ef1c9197"),
    serviceId: ObjectId("572e29b0116b5db3057bd821"),
    status: 'completed'
  } 
  </code></pre>
  
  <p>I have an aggregate operation on <strong>requests</strong> collection which returns documents in the following format:</p>
  
  <pre><code>{
      "grossRequests" : 2,
      "grossData" : [ 
          {
              "date" : "2018-08-04",
              "count" : 1,
              "requests" : [ 
                  ObjectId("5b658147c73beb5ea3debc6e")
              ]
          }, 
          {
              "date" : "2018-08-05",
              "count" : 1,
              "requests" : [ 
                  ObjectId("5b658160572faa5dd033fb48")
              ]
          }
      ],
      "netData" : [ 
          {
              "date" : "2018-08-05",
              "count" : 1,
              "requests" : [ 
                  ObjectId("5b658160572faa5dd033fb48")
              ]
          }
      ],
      "netRequests" : 1.0,
      "userId" : ObjectId("5c1239e93a7b9a72ef1c9197"),
      "serviceId" : "572e29b0116b5db3057bd821"
  }
  </code></pre>
  
  <p>Above is the document to be inserted in <strong>cumulativeData</strong></p>
  
  <p>Now, I need to add all the documents returned by my aggregation operation, into a collection called <strong>cumulativeData</strong>.</p>
  
  <p>The <strong>cumulativeData</strong> collection has one document <strong>per userId per serviceType</strong>.</p>
  
  <p>I am running a query for specific date ranges and while inserting them into the collection, would like to "merge" the documents, rather than replacing them.</p>
  
  <p>so, for example, if I am looping through all the documents returned by the aggregation operation using forEach, for each new document I get (<code>{userId,serviceType}</code> pair which is not present in the <strong>cumulativeData</strong> collection) I need to insert it as is and for every document which is already present in the collection, I need to update it as follows.</p>
  
  <pre><code>grossRequests -&gt; add both values
  grossData -&gt; push the new values into the existing set
  netData -&gt; push the new values into the existing set
  netRequests -&gt; add both values
  </code></pre>
  
  <p>The operation would be as follows,</p>
  
  <p>Existing doc in ** cumulativeData **</p>
  
  <pre><code>{
      "grossRequests": 2,
      "grossData": [{
              "date": "2018-08-04",
              "count": 1,
              "requests": [
                  ObjectId("5b658147c73beb5ea3debc6e")
              ]
          }, {
              "date": "2018-08-05",
              "count": 1,
              "requests": [
                  ObjectId("5b658160572faa5dd033fb48")
              ]
          }
      ],
      "netData": [{
              "date": "2018-08-05",
              "count": 1,
              "requests": [
                  ObjectId("5b658160572faa5dd033fb48")
              ]
          }
      ],
      "netRequests": 1,
      "userId": ObjectId("5c1239e93a7b9a72ef1c9197"),
      "serviceId": "572e29b0116b5db3057bd821"
  }
  </code></pre>
  
  <p>New Document generated after aggregation on a new date range</p>
  
  <pre><code>{
      "grossRequests": 2,
      "grossData": [{
              "date": "2018-08-04",
              "count": 1,
              "requests": [
                  ObjectId("5b658147c73beb5ea3debc8e")
              ]
          }, {
              "date": "2018-08-05",
              "count": 1,
              "requests": [
                  ObjectId("5b658160572faa5dd033fb4l")
              ]
          }
      ],
      "netData": [{
              "date": "2018-08-05",
              "count": 1,
              "requests": [
                  ObjectId("5b658160572faa5dd033fb4l")
              ]
          }
      ],
      "netRequests": 1,
      "userId": ObjectId("5c1239e93a7b9a72ef1c9197"),
      "serviceId": "572e29b0116b5db3057bd821"
  }
  </code></pre>
  
  <p>Final Result</p>
  
  <pre><code>{
      "grossRequests": 4,
      "grossData": [{
              "date": "2018-08-04",
              "count": 2,
              "requests": [
                  ObjectId("5b658147c73beb5ea3debc6e") , ObjectId("5b658147c73beb5ea3debc8e")
              ]
          }, {
              "date": "2018-08-05",
              "count": 2,
              "requests": [
                  ObjectId("5b658160572faa5dd033fb48"), ObjectId("5b658160572faa5dd033fb4l")
              ]
          }
      ],
      "netData": [{
              "date": "2018-08-05",
              "count": 2,
              "requests": [
                  ObjectId("5b658160572faa5dd033fb48"),ObjectId("5b658160572faa5dd033fb4l")
              ]
          }
      ],
      "netRequests": 2,
      "userId": ObjectId("5c1239e93a7b9a72ef1c9197"),
      "serviceId": "572e29b0116b5db3057bd821"
  }
  </code></pre>