public static DateTime GetDateTime(this BsonDocument doc, string fieldName, bool dateOnly = false) { DateTime def = Tools.DefaultDate; DateTime dt = def; string[] fields = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length == 1) { dt = Tools.DefaultDate; var val = doc.GetValue(fieldName, dt); if (val.IsValidDateTime) { dt = val.ToUniversalTime(); } else { dt = Tools.ToDateTime(val.ToString(), dateOnly); } } else { var newindex = String.Join(".", fields.Skip(1).ToArray()); dt = BsonHelper.GetDateTime(doc.GetValue(fields[0]).AsBsonDocument, newindex, dateOnly); } if (dateOnly) { dt = Tools.ToDateTime(String.Format("{0:dd-MMM-yyyy}", dt)); } return(dt); }
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 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 BsonDocument GetDoc(this BsonDocument doc, string fieldName, BsonDocument defaultValue = null) { string[] fields = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length == 1) { var value = doc.GetValue(fieldName, defaultValue); return(value.IsBsonDocument ? value.ToBsonDocument() : defaultValue); } else { var newindex = String.Join(".", fields.Skip(1).ToArray()); return(BsonHelper.GetDoc(doc.GetValue(fields[0]).AsBsonDocument, newindex, defaultValue)); } }
internal static List <BsonDocument> AggregateFromFile(string collection, string file, FilterDefinition <BsonDocument> q = null, BsonDocument docReplace = null) { //List<BsonDocument> pipesAggr = BsonHelper.ListDocFromFile(file,docReplace); List <BsonDocument> pipes = new List <BsonDocument>(); if (q != null) { pipes.Add(new BsonDocument().Set("$match", q.ToBsonDocument())); } pipes.AddRange(BsonHelper.ListDocFromFile(file, docReplace)); var results = DataHelper.Aggregate(collection, pipes); return(results); }
public static Int64 GetInt64(this BsonDocument doc, string fieldName, Int64 defaultValue = 0) { //return doc.GetValue(fieldName, defaultValue).ToInt64(); string[] fields = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length == 1) { var val = doc.GetValue(fieldName, defaultValue); return(val.IsNumeric ? val.ToInt64() : defaultValue); } else { var newindex = String.Join(".", fields.Skip(1).ToArray()); return(BsonHelper.GetInt64(doc.GetValue(fields[0]).AsBsonDocument, newindex, defaultValue)); } }
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)); } }
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); } }
public static Double GetDouble(this BsonDocument doc, string fieldName, Double defaultValue = 0) { try { double ret = 0; string[] fields = fieldName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length == 1) { var val = doc.GetValue(fieldName); if (val.IsNumeric) { ret = val.ToDouble(); } else { var str = ""; var source = val.ToString(); var allowed = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-' }; foreach (char c in source) { if (allowed.Contains(c)) { str += c; } } ret = Tools.ToDouble(str); } } else { var newindex = String.Join(".", fields.Skip(1).ToArray()); ret = BsonHelper.GetDouble(doc.GetValue(fields[0]).AsBsonDocument, newindex, defaultValue); } return(ret); } catch (Exception e) { return(defaultValue); } }
public void Save(string tablename = "", Func <BsonDocument, BsonDocument> fnUpdate = null, bool runPreSave = true, bool runPostSave = true, string[] references = null) { if (tablename.Equals("")) { tablename = TableName; } this.LastUpdate = DateTime.Now; var doc = runPreSave ? PreSave(this.ToBsonDocument(), references) : this.ToBsonDocument(); if (fnUpdate != null) { doc = fnUpdate(doc); } if (BsonHelper.HasElement(doc, "_t")) { doc.RemoveElement(doc.GetElement("_t")); } DataHelper.Save(tablename, doc); if (runPostSave) { doc = PostSave(doc, references); } }
public static DateIsland GetDateInfo(this BsonDocument d, string field) { DateTime dt = BsonHelper.GetDateTime(d, field); return(new DateIsland(dt)); }