public static BsonDocument FlattenMapReduceOutput(this BsonDocument doc) { BsonDocument ret = new BsonDocument(); var idIsDoc = doc.GetValue("_id", 0).IsBsonDocument; var valueIsDoc = doc.GetValue("value", 0).IsBsonDocument; if (idIsDoc) { BsonDocument docId = doc.GetValue("_id").AsBsonDocument; foreach (var el in docId.Elements) { ret.Set(el.Name, BsonHelper.Get(docId, el.Name)); } } else { ret.Set("_id", doc.GetValue("_id")); } if (valueIsDoc) { BsonDocument docValue = doc.GetValue("value").AsBsonDocument; foreach (var el in docValue.Elements) { ret.Set(el.Name, BsonHelper.Get(docValue, el.Name)); } } else { ret.Set("value", doc.GetValue("value", 0)); } return(ret); }
public static BsonValue Get(this BsonDocument doc, string fieldName) { try { string[] fields = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length == 1) { return(doc.GetValue(fieldName)); } else { var newindex = String.Join(".", fields.Skip(1).ToArray()); return(BsonHelper.Get(doc.GetValue(fields[0]).AsBsonDocument, newindex)); } } catch (Exception e) { return(BsonNull.Value); } }