public static List <BsonDocument> Find(IEnumerable <BsonDocument> docs, BsonDocument doc, IEnumerable <string> fields) { List <BsonDocument> ret = null; List <BsonDocument> sources = docs.ToList(); int fieldCount = fields.Count(); int i = 0; bool loop = true; while (loop && i < fieldCount) { string field = fields.ElementAt(i); ret = sources.Where(d => BsonHelper.GetString(d, field).Equals(BsonHelper.GetString(doc, field))).ToList(); if (ret == null || ret.Count == 0) { loop = false; } else { sources = ret; } i++; } if (ret == null) { ret = new List <BsonDocument>(); } return(ret); }
public static string GetString(this BsonDocument doc, string fieldName, string defaultValue = "") { string[] fields = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length == 1) { var val = doc.GetValue(fieldName, ""); string strVal = val.ToString(); if (String.IsNullOrEmpty(strVal) && defaultValue != "") { strVal = defaultValue; } return(strVal); } else { var newindex = String.Join(".", fields.Skip(1).ToArray()); return(BsonHelper.GetString(doc.GetValue(fields[0]).AsBsonDocument, newindex, defaultValue)); } }