title: Insert items into dictionary in MongoDB via C#
body: <p>I am unable to translate working Mongo shell commands into C# code. Specifically I would like to insert a new item into a dictionary using either the  ArrayOfDocument or ArrayOfArray representation. In the example below I'd like to insert a new grade into my Pupil class.</p>
  
  <p>C# Pupil class</p>
  
  <pre><code>[BsonId]
  public string Name;
  [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
  public Dictionary&lt;string, int&gt; Grades;
  </code></pre>
  
  <p>Mongo shell works</p>
  
  <pre><code>db.myPupils.update( {"_id":"Tom"}, {$addToSet: {"Grades": {"k":"Math","v":5}}})
  </code></pre>
  
  <p>However in C# <s>I get "Specific cast is not valid"</s> <em>the Linq syntax does the trick</em>.</p>
  
  <pre><code>var kvp = new KeyValuePair&lt;string, int&gt;("Math",5);
  var res = collection.UpdateOne(
          Builders&lt;Pupil&gt;.Filter.Eq("_id", "Tom"),
          //Builders&lt;Pupil&gt;.Update.AddToSet("Grades", kvp.ToBsonDocument()));
          Builders&lt;Pupil&gt;.Update.AddToSet(k =&gt; k.Grades, kvp));
  </code></pre>
  
  <p>I've also tried the same using <code>ArrayOfArrays</code> , Mongo shell works fine again:</p>
  
  <pre><code>db.myPupils.update( {"_id":"Tom"}, {$addToSet: {"Grades": ["Math",5]}})
  </code></pre>
  
  <p>C# gives "The serializer for field "Grades" must implement IBsonArraySerializer and provide item serilization info."</p>
  
  <pre><code>var res = collection.UpdateOne(
          Builders&lt;Pupil&gt;.Filter.Eq("_id", "Tom"),
          Builders&lt;Pupil&gt;.Update.AddToSet("Grades", new BsonArray{"Math",5});
  </code></pre>
  
  <p>What am I missing? Many Thanks for your help.</p>
  
  <p><strong>Update</strong></p>
  
  <p>For ArrayOfDocuments, the Linq syntax does the trick. Still no success for the ArrayOfArrays bit though...</p>