title: How to convert mongo document into key value pair in .Net?
body: <p>I am trying to query MongoDB collection using C# .Net. I want to filter the collection based on some criteria, then project only certain fields, and then convert the projection into key-value pair.</p>
  
  <p>I was able to filter and project the way I wanted. However I want to know is there in built method to convert projection into key-value pair? </p>
  
  <p>Here is how my mongo DB document  looks like</p>
  
  <pre><code>{
      "_id" : 32178271832,
      "url" : "www.somedomain.com",
      "fileName" : "somefilename.pdf",
      "isDone" : true,
      "client" : ObjectId("56g2e67d7gf2208c2dbe33yt"),
      "startTime" : ISODate("2015-05-23T18:50:11.271Z"),
      "endTime" : ISODate("2015-05-23T18:52:43.819Z"),
      "cost" : 30,
      "taskGroups" : [ 
          {
              "phase" : "phase1",
              "name" : "Some Name",    
              "_id" : ObjectId("43r2e46h7er2208c2geh74e5"),
              "tasks" : [ 
                  {
                      "_id" : ObjectId("43r2e46h7er2208c2geh74e5"),
                      "input" : [],                 
                      "predecessors" : []
                  }
              ]
          }        
      ],
      "context" : {
          "startTime" : ISODate("2016-03-23T18:50:11.271Z"),
          "endTime" : ISODate("2016-03-23T18:52:43.819Z"),
          "state" : "GA",       
          "PHASE1" : {
              "hasaccountnumber" : "Yes",
              "accountnumber" : 1
              "Child1":{
                  "Property1": "SomeValue1"
                  "Property2" : "SomeValue2"
              }
          }        
      }
  }
  </code></pre>
  
  <p>Here is my code</p>
  
  <pre><code>public static IDictionary&lt;string,string&gt; GetFromMongo()
      {
          var collection = _mongodb.GetCollection&lt;BsonDocument&gt;("kunits");
          var filterBuilder = Builders&lt;BsonDocument&gt;.Filter;
          var filter = filterBuilder.Eq&lt;bool&gt;("isDone", true) &amp; filterBuilder.Exists("isTransformed", false);
  
          var projection = Builders&lt;BsonDocument&gt;.Projection
              .Include("client")
              .Include("url")
              .Include("fileName")
              .Include("context");
  
          var document = collection.Find(filter).Project(projection).FirstOrDefault();
  
          IDictionary&lt;string,string&gt; dictionary = ??
  
          return dictionary;
      }
  </code></pre>
  
  <p>Is there anyway to convert document into dictionary? The document has hierarchical structure. </p>
  
  <p><strong>Update2</strong><br>
  So this is what I have done. However how do I recursively process if the <code>BsonValue</code> is JSON? For example in the above JSON <code>context</code> element has hierarchal structure, how do I recursively loop and add each of its field in the dictionary? </p>
  
  <pre><code>         var document = collection.Find(filter).Project(projection).FirstOrDefault();
          var docs = new Dictionary&lt;string, BsonValue&gt;();
          foreach (var elm in document.Elements)
          {
              //how to recursively process when elm.value is JSON ??
  
              docs.Add(elm.Name, elm.Value);
  
          }
  </code></pre>