title: MongoDB Aggregate function in C#
body: <p>I am trying to display/list data after using aggregation function but it isn't happening.</p>
  
  <p>This code works absolutely fine.</p>
  
  <pre><code>        var connectionstring = "mongodb://localhost:27017";
          var client = new MongoClient(connectionstring);
          var db = client.GetDatabase("school");
          var col = db.GetCollection&lt;BsonDocument&gt;("students");
          var filter = new BsonDocument("type", "homework");
          var filter2 = Builders&lt;BsonDocument&gt;.Filter.Eq("scores.type", "homework");
  
          var myresults = await col.Find(filter2)
              .Limit(2)
              .Project("{name:1,scores:1,_id:0}")
              .Sort("{score:1}")
              .ToListAsync();
  
          foreach (var result in myresults)
          {
              Console.WriteLine(result);
          }
  </code></pre>
  
  <p>This code fetches document as it should however when I replace </p>
  
  <pre><code> var myresults = await col.Find(filter2)
              .Limit(2)
              .Project("{name:1,scores:1,_id:0}")
              .Sort("{score:1}")
              .ToListAsync();
  </code></pre>
  
  <p>with this</p>
  
  <pre><code>            var myresults = await col.Aggregate()
              .Unwind("{$scores}")
              .Group(new BsonDocument { { "_id", "$_id" }, { "lowscore", new BsonDocument("$min", "$scores.score") } })
              //.Group("{_id:'$_id',lowscore:{$min:'$scores.score'}}")
              .ToListAsync();
  </code></pre>
  
  <p>No record is being pulled. 
  I do not want to use Pipeline method. I simply want to display the result obtained via aggregate function.</p>
  
  <p>This is my Mongo Query (I want the same result as this in C#)-</p>
  
  <pre><code>db.students.aggregate([{$sort:{_id:-1}},{$unwind:"$scores"},{$group:{_id:"$_id", lowscore:{"$min":"$scores.score"}}}])
  </code></pre>