public Dictionary ToDictionary(BsonDocument document) { var dictionary = new Dictionary(document.ElementCount); foreach (var element in document.Elements) { dictionary.Add(element.Name, ToDictionaryHelper(element.Value)); } return dictionary; } private object ToDictionaryHelper(BsonValue value) { switch (value.BsonType) { case BsonType.Array: return ToArrayHelper(value.AsBsonArray); case BsonType.Document: return value.AsBsonDocument.ToDictionary(); case BsonType.DateTime: return value.AsDateTime; default: return value.RawValue; } } private object ToArrayHelper(BsonArray value) { var casted = value.AsBsonArray; var array = new object[value.AsBsonArray.Count]; for (int i = 0; i < array.Length; ++i) { if (casted[i].IsBsonDocument) { array[i] = ToDictionary(casted[i] as BsonDocument); } else if (casted[i].IsBsonArray) { array[i] = ToArrayHelper(casted[i].AsBsonArray); } else { array[i] = casted[i].RawValue; } } return array; } public void TestToDictionary() { Dictionary testDict = new Dictionary { { "Name", "Test" }, { "Num", 1 }, { "Date", DateTime.Now }, { "Nested", new object[] { new Dictionary { { "Name", "One" }, { "Value", 1 } }, new Dictionary { { "Name", "Two" }, { "Value", 2 } }, new Dictionary { { "Name", "Three" }, { "Value", 3 } } }}, { "Array", new object[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 }, new object[] { 7, 8, 9 } }} }; var document = BsonDocument.Create(testDict); Dictionary converted = document.ToDictionary(); object[] array = converted["Nested"] as object[]; Assert.AreEqual(3, array.Length); Assert.IsNotNull(array[0]); Dictionary castedDict = array[0] as Dictionary; Assert.IsNotNull(castedDict); Assert.AreEqual("One", castedDict["Name"]); Assert.AreEqual(1, castedDict["Value"]); array = converted["Array"] as object[]; Assert.AreEqual(3, array.Length); Assert.IsNotNull(array[0]); object[] castedArray = array[0] as object[]; Assert.IsNotNull(castedArray); Assert.IsTrue(castedArray.Contains(1)); Assert.IsTrue(castedArray.Contains(2)); Assert.IsTrue(castedArray.Contains(3)); }