title: C# Mongodb. Sum field for all documents
body: <p>I need to calculate total for all document in the collection. For example, I have the following three documents:  </p>
  
  <pre><code>_id: 1,
  clientId: 1,
  cash: 500
  
  _id: 2,
  clientId: 1,
  cash: 100
  
  _id: 3,
  clientId: 2,
  cash: 200
  </code></pre>
  
  <p>So, I want to sum <code>cash</code> field for the specific <code>clientId</code>. For the client 1 I want to get <code>600</code>, for the client 2 I wanto get <code>200</code>. It's my first usage of aggregation frameworkd in MongoDB. I checked documentation and the other questions from SO, but can't figure out, why I have to use <code>group</code> clause. Why do I need it, if just want to sum. I event tried to use <code>Group</code> but can't get it compile:</p>
  
  <pre><code>_collection.Aggregate().Match(t=&gt;t.ClientId == 1).Group(null, g =&gt; new
              {
                  Total = g.Sum(o =&gt; o.Cash)
              });
  </code></pre>
  
  <blockquote>
    <p>No overload for method 'Group' takes 2 arguments</p>
  </blockquote>
  
  <p>PS. I don't want to use <code>BsonDocument</code> and such thing like <code>$sum</code> keywords. I want to make it in more C# way.</p>