title: Mongodb C# driver, simple filtering and then group result by property
body: <p>I am trying to filter mongo results by some properties and then group them by property <code>Name</code>. Any help would be greatly appreciated.</p>
  
  <p>This what I have in my mongo collection:</p>
  
  <pre><code>[
    {
      Name: "test1",
      Type: "typeX"
      Date: "10-10-10",
      Age: 10,
      Value: 20
    },
    {
      Name: "test1",
      Type: "typeX"
      Date: "10-10-10",
      Age: 1000,
      Value: 2000
    },
    {
      Name: "test2",
      Type: "typeX"
      Date: "10-10-10",
      Age: 20,
      Value: 30
    },
    {
      Name: "test3",
      Type: "typeX"
      Date: "10-10-10",
      Age: 30,
      Value: 40
    }
  ]
  </code></pre>
  
  <p>Filter step: <code>if</code> <code>Date = "10-10-10"</code> and <code>Type = "typeX"</code> and <code>Name $in ["test1", "test2"]</code>. This step works correcly and I am able to filter the results.</p>
  
  <pre><code>[
    {
      Name: "test1",
      Type: "typeX"
      Date: "10-10-10",
      Age: 10,
      Value: 20
    },
    {
      Name: "test1",
      Type: "typeX"
      Date: "10-10-10",
      Age: 1000,
      Value: 2000
    },
    {
      Name: "test2",
      Type: "typeX"
      Date: "10-10-10",
      Age: 20,
      Value: 30
    }
  ]
  </code></pre>
  
  <p>Group the filtered result by <code>Name</code>: this step does not work.</p>
  
  <pre><code>{
    "test1": [
      {
        Name: "test1",
        Type: "typeX"
        Date: "10-10-10",
        Age: 10,
        Value: 20
      },
      {
        Name: "test1",
        Type: "typeX"
        Date: "10-10-10",
        Age: 1000,
        Value: 2000
      }
    ],
    "test2": [
      {
        Name: "test2",
        Type: "typeX"
        Date: "10-10-10",
        Age: 20,
        Value: 30
      }
    ]
  }
  </code></pre>
  
  <p>The following is my code so far. I really want to avoid using lambda expression because I have seen substantial performance loss whenever I tried it.</p>
  
  <pre><code>public Dictionary&lt;string, List&lt;DummyClass&gt;&gt; FilterAndGroupBy(string type, string date, List&lt;string&gt; names)
  {
      // Get the collection
      var collection = GetDummyCollection();
  
      var filter = new BsonDocument
      {
          {"Date", date},
          {"Type", type}
      };
  
      // Create the filter
      filter.Add("Name", new BsonDocument
      {
          {"$in", new BsonArray(indexNames)}
      });
  
      // Create the group, this does not work.
      var group = new BsonDocument
      {
          {"_id",
              new BsonDocument {
                  {"Name", "$Name"},
                  {"Result", new BsonDocument("$push", new BsonArray()) }
              }
          }
      };
  
      // Query the group with the filter
      var resultslambda = collection.Aggregate().Match(filter).Group(group).ToList();
  
      return new Dictionary&lt;string, List&lt;DummyClass&gt;&gt;();
  }
  </code></pre>