title: Unwind then Group aggregation in MongoDB C#
body: <p>I'm having some trouble with the new C# 2.0 MongoDB driver and the aggregation pipeline.</p>
  
  <p>Basically, I'm trying to return the most popular elements within an array field on the object. The field type is: <code>IList&lt;string&gt; FavouritePlaceIds { get; set; }</code>.</p>
  
  <p>I have the following MongoDB aggregation which is working as expected:</p>
  
  <pre><code>db.users.aggregate([
      { $unwind : "$FavouritePlaceIds" },
      { $group: { "_id": "$FavouritePlaceIds", "count": {$sum: 1}}}, 
      { $sort : { "count": -1 }}
  ])
  </code></pre>
  
  <p>However, the issue is now trying to translate that into C# code using the new MongoDB driver 2.0. I've been using the following link for help with the aggregation pipeline: <a href="http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/crud/reading/#unwind" rel="noreferrer">http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/crud/reading/#unwind</a></p>
  
  <p>I've got the following so far for my aggregation pipeline:</p>
  
  <pre><code>var pipeline = usersCollection.Aggregate()
                  .Unwind(i =&gt; i.FavouritePlaceIds)
                  .Group(i =&gt; i.FavouritePlaceIds, g =&gt; new { FavouritePlaceIds = g.Key, Count = g.Count() })
                  .SortByDescending(i =&gt; i.Count);
  </code></pre>
  
  <p>When I go to compile that code, I get following message: </p>
  
  <blockquote>
    <p>'BsonDocument' does not contain a definition for 'FavouritePlaceIds' and no extension method 'FavouritePlaceIds' accepting a first argument of type 'BsonDocument' could be found...</p>
  </blockquote>
  
  <p>The error is occuring on the first parameter (<code>i =&gt; i.FavouritePlaceIds</code>) of the Group() method. </p>
  
  <p>Reading the notes at the link provided under the group section, it mentions that:</p>
  
  <blockquote>
    <p>Because $unwind is a type of projection, you must provide a return type.</p>
  </blockquote>
  
  <p>So, I'm assuming that I'm not specifying a correct return type, which is why it's expecting a BsonDocument object, and failing to compile. </p>
  
  <p>So, how can I specify the correct return type to use in the Group method?</p>