I created a straight-forward extension to the MongoDB driver that re-serializes the bson document using JSON.Net and deserializes it as a dynamic. By inlcuding the following class, you can simply convert your mongo queries to dynamic like this
dynamic obj = collection.FindOneByIdAs<BsonDocument>(someObjectId).ToDynamic();
Extension class:
public static class MongoDynamic
{
private static System.Text.RegularExpressions.Regex objectIdReplace = new System.Text.RegularExpressions.Regex(@"ObjectId\((.[a-f0-9]{24}.)\)", System.Text.RegularExpressions.RegexOptions.Compiled);
/// <summary>
/// deserializes this bson doc to a .net dynamic object
/// </summary>
/// <param name="bson">bson doc to convert to dynamic</param>
public static dynamic ToDynamic(this BsonDocument bson)
{
var json = objectIdReplace.Replace(bson.ToJson(), (s) => s.Groups[1].Value);
return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
}
}
Be sure to reference Newtonsoft.Json.dll (http://james.newtonking.com/projects/json-net.aspx)