title: Aggregate $substr in Mongodb C# Driver
body: <p>I starting using a MongoDB in my application. I im using Robo 3T to learn and test the queries and now, I'm translating the queries to C#.  </p>
  
  <p><strong>MY QUERY ON MONGO ROBO 3T</strong></p>
  
  <pre><code>db.dbCENTRAL.aggregate([
            { 
                $match: { PartnerId: "2021", DATAINST: {$gte: "2018-01-01 00:00:00", $lte: "2019-03-12 23:59:59"}, }   
            },
  
            { 
                 $group: { _id:  { $substr : ["$DATAINST", 0, 10 ] }, count: { $sum: 1 }}
            }
  ])
  </code></pre>
  
  <p>Running the script above on Robo 3T its retrieved this result:</p>
  
  <pre><code>{
      "_id" : "2018-01-10",
      "count" : 1.0
  }
  
  /* 2 */
  {
      "_id" : "2018-02-09",
      "count" : 1.0
  }
  
  /* 3 */
  {
      "_id" : "2018-02-26",
      "count" : 1.0
  }
  
  /* 4 */
  {
      "_id" : "2018-03-02",
      "count" : 1.0
  }
  
  /* 5 */
  {
      "_id" : "2018-03-08",
      "count" : 1.0
  }
  </code></pre>
  
  <p>Now, im trying to reproduce this result on my .NET MVC project and I'm facing difficult to translate the substring to C#.</p>
  
  <p><strong>C# CODE</strong></p>
  
  <pre><code>var connString = "mongodb+srv:";
  var client = new MongoClient(connString);
  var db = client.GetDatabase("SyncMaster");
  var collection = db.GetCollection&lt;BsonDocument&gt;("dbCENTRAL");
  
  var match1 = new BsonDocument("$match", new BsonDocument("PartnerId", "2021"));
  
  var match2 = new BsonDocument { { "$match", new BsonDocument { { "DATAINST", new BsonDocument { { "$gte", data1 }, { "$lt", data2 } } } } } };
  
  var group = new BsonDocument
                  {
                      { "$group",
                          new BsonDocument
                              {
                                  { "_id", new BsonDocument
                                               {
                                                   {
                                                       "Dia", "$DATAINST"
                                                   }
                                               }
                                  },
                                  {
                                      "Count", new BsonDocument
                                                   {
                                                       {
                                                           "$sum", 1
                                                       }
                                                   }
                                  }
                              }
                    }
                  };
  
  var pipeline = new[] { match1, match2, group };
  
  var result = collection.Aggregate&lt;BsonDocument&gt;(pipeline).ToList();
  </code></pre>
  
  <p>That way is working fine, but, it's grouping by entire string Date, and I would like to group by short string date like: "2019-03-01".</p>
  
  <p>I tried this way, but not worked, it returned _id: null</p>
  
  <pre><code>{
     "Dia", "$substr : [$DATAINST, 0, 10 ]"
  }
  </code></pre>
  
  <p>Then I tried this:</p>
  
  <pre><code> {
     "Dia", new BsonDocument {
  
                                 {
                                     "$substr",  "[$DATAINST, 0, 10 ]"
  
                                  }
                              }
    }
  </code></pre>
  
  <p>I got this error:
  <strong>MongoDB.Driver.MongoCommandException: 'Command aggregate failed: Expression $substrBytes takes exactly 3 arguments. 1 were passed in..'</strong></p>
  
  <p>I also tryed:</p>
  
  <pre><code>{
     "$substr",  "[\"$DATAINST\", 0, 10 ]"
  }
  </code></pre>