title: Mongoose aggregation "$sum" of rows in sub document
body: <p>I'm fairly good with sql queries, but I can't seem to get my head around grouping and getting sum of mongo db documents,</p>
  
  <p>With <a href="https://stackoverflow.com/questions/29088437/mongoose-sum-aggregation">this</a> in mind, I have a job model with schema like below :</p>
  
  <pre><code>    {
          name: {
              type: String,
              required: true
          },
          info: String,
          active: {
              type: Boolean,
              default: true
          },
          all_service: [
  
              price: {
                  type: Number,
                  min: 0,
                  required: true
              },
              all_sub_item: [{
                  name: String,
                  price:{ // &lt;&lt; -- this is the price I want to calculate
                      type: Number,
                      min: 0
                  },
                  owner: {
                      user_id: {  //  &lt;&lt;-- here is the filter I want to put
                          type: Schema.Types.ObjectId,
                          required: true
                      },
                      name: String,
                      ...
                  }
              }]
  
          ],
          date_create: {
              type: Date,
              default : Date.now
          },
          date_update: {
              type: Date,
              default : Date.now
          }
      }
  </code></pre>
  
  <p>I would like to have a sum of <code>price</code> column, where <code>owner</code> is present, I tried below but no luck</p>
  
  <pre><code> Job.aggregate(
          [
              {
                  $group: {
                      _id: {}, // not sure what to put here
                      amount: { $sum: '$all_service.all_sub_item.price' }
                  },
                  $match: {'not sure how to limit the user': given_user_id}
              }
          ],
          //{ $project: { _id: 1, expense: 1 }}, // you can only project fields from 'group'
          function(err, summary) {
              console.log(err);
              console.log(summary);
          }
      );
  </code></pre>
  
  <p>Could someone guide me in the right direction. thank you in advance</p>