title: MongoDB Aggregation Pipeline C#
body: <p>I have the following Mongo query:</p>
  
  <pre><code>db.BusRatings.aggregate(
  [     
     { $match: { VId: 2020}},
     { $project: {  vid: '$VId', sb:'$SvcRating.StaffBehavior'  ,bq: '$SvcRating.BusQuality' ,  src: '$SvcDetails.SrcNm', dst: '$SvcDetails.DestNm', py: '$SvcRating.Punctuality'}},    
     { $group: { _id: {vid: '$vid', src: '$src', dst: '$dst'}, numratings: {$sum: 1}, avgpy : { $avg : '$py'}, avgbq: { $avg: '$bq'},avgsb: { $avg: '$sb'}} },
     { $match : { numratings: { $gt: 3 } } },
     { $project: {_id: 0,  'vid' : '$_id.vid',  'src' : '$_id.src',  'dst' : '$_id.dst', 'avg': {'$divide':[ {'$add': [ '$avgpy', '$avgbq', '$avgsb' ]},3.0]}}},
     { $sort : { avg : -1 } }
   ]
  )
  </code></pre>
  
  <p>What would be the C# code to do the last projection in the pipeline above. I am following the example listed at the documentation here:</p>
  
  <p><a href="http://mikaelkoskinen.net/mongodb-aggregation-framework-examples-in-c/" rel="nofollow">http://mikaelkoskinen.net/mongodb-aggregation-framework-examples-in-c/</a></p>
  
  <p>I have the following code for the first match,project,group  :</p>
  
  <pre><code> var match1 = new BsonDocument 
                  { 
                      { 
                          "$match", 
                          new BsonDocument 
                              { 
                                  {"VId", opId} 
                              } 
                      } 
                  };
  
  
              var project1 = new BsonDocument 
                  { 
                      { 
                          "$project", 
                          new BsonDocument 
                              { 
                                  {  "vid", opId},
                                  {  "sb", "$SvcRating.StaffBehavior"},
                                  {   "bq", "$SvcRating.BusQuality"},
                                  {   "src",   "$SvcDetails.SrcNm"},
                                  {   "dst", "$SvcDetails.DestNm"} ,
                                  {   "py",  "$SvcRating.Punctuality"}
                              } 
                      } 
                  };
  
              var group = new BsonDocument 
                  { 
                      { 
                          "$group", 
                          new BsonDocument 
                              { 
                                  { "_id", new BsonDocument 
                                               { 
                                                  {"vid", "$vid"},
                                                  { "src", "$src"},
                                                  { "dst", "$dst"}
                                               } 
                                  }, 
                                  { 
                                      "numratings", new BsonDocument 
                                                   { 
                                                       { "$sum", 1 } 
                                                   } 
                                  }, 
                                  {
                                      "avgpy", new BsonDocument
                                                {
                                                     { "$avg" , "$py"}
                                                }
                                  }
                              }
                              }
                  };
  
              var match2 = new BsonDocument 
                  { 
                      { 
                          "$match", 
                          new BsonDocument 
                              { 
                                  {"numratings", new BsonDocument
                                      {
                                          {"$gt",3}
                                      }    
                                  } 
                              } 
                      } 
                  };
  </code></pre>