private object GetValue(BsonValue value) { if (value.IsString) { return(value.AsString); } else if (value.IsInt32) { return(value.AsInt32); } else if (value.IsInt64) { return(value.AsInt64); } else if (value.IsBoolean) { return(value.AsBoolean); } else if (value.IsDouble) { return(value.AsDouble); } else if (value.IsValidDateTime) { return((long)value.ToLocalTime().Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds); } //else if (value.IsObjectId) // itemValue = value.AsObjectId.ToString(); return(null); }
/// <summary> /// BsonValue转展示用字符 /// </summary> /// <param name="bsonValue"></param> /// <returns></returns> public static string ConvertToString(BsonValue bsonValue) { //二进制数据 if (bsonValue.IsBsonBinaryData) { return("[Binary]"); } //空值 if (bsonValue.IsBsonNull) { return("[Empty]"); } //文档 if (bsonValue.IsBsonDocument) { return(bsonValue + "[Contains" + bsonValue.ToBsonDocument().ElementCount + "Documents]"); } //时间 if (bsonValue.IsValidDateTime) { if (IsUTC) { return(bsonValue.ToUniversalTime().ToString()); } else { //@flydreamer提出的本地化时间要求 return(bsonValue.ToLocalTime().ToString()); } } //字符 if (bsonValue.IsString) { //只有在字符的时候加上"" return("\"" + bsonValue + "\""); } //其他 return(bsonValue.ToString()); }
private static object Convert(BsonValue value, Type type) { switch (type.Name) { case "String": return(value.ToString()); case "Int32": return(value.ToInt32()); case "Int64": return(value.ToInt64()); case "Decimal": return((decimal)value.ToDouble()); case "Double": return(value.ToDouble()); case "DateTime": return(value.ToLocalTime()); case "Boolean": return(value.ToBoolean()); case "Nullable`1": var nullType = type.GetProperty("Value").PropertyType; switch (nullType.Name) { case "DateTime": return(value.ToNullableLocalTime()); default: return(Convert(value, nullType)); } } return(value); }
private object Bson2Object(BsonValue bd) { object rtn = null; if (bd.IsBsonNull) { rtn = null; } else if (bd.IsBsonDateTime) { //mongo存储的实际均为UTC标准时间,系统在运行时都是本地化的时间 rtn = bd.ToLocalTime(); } else if (bd.IsBsonJavaScript) { rtn = bd.AsBsonJavaScript.ToString(); } else if (bd.IsBsonArray) { var arr = bd.AsBsonArray; var list = new List <object>(); foreach (var item in arr) { list.Add(Bson2Object(item)); } rtn = list.ToArray(); } else if (bd.IsBsonDocument) { rtn = Bson2Dynamic(bd.AsBsonDocument); } else if (bd.IsObjectId) { rtn = bd.AsObjectId.Pid; } else if (bd.IsInt32) { rtn = bd.AsInt32; } else if (bd.IsInt64) { rtn = bd.AsInt64; } else if (bd.IsDouble) { rtn = bd.AsDouble; } else if (bd.IsNumeric) { rtn = decimal.Parse(bd.ToString()); } else if (bd.IsBoolean) { rtn = bd.ToBoolean(); } else { rtn = bd.AsString; } return(rtn); }
/// <summary> /// BsonValue转展示用字符 /// </summary> /// <param name="bsonValue"></param> /// <returns></returns> public static string ConvertToString(BsonValue bsonValue) { //二进制数据 if (bsonValue.IsBsonBinaryData) { return "[Binary]"; } //空值 if (bsonValue.IsBsonNull) { return "[Empty]"; } //文档 if (bsonValue.IsBsonDocument) { return bsonValue + "[Contains" + bsonValue.ToBsonDocument().ElementCount + "Documents]"; } //时间 if (bsonValue.IsValidDateTime) { if (IsUtc) { return bsonValue.ToUniversalTime().ToString(); } //@flydreamer提出的本地化时间要求 return bsonValue.ToLocalTime().ToString(); } //字符 if (bsonValue.IsString) { //只有在字符的时候加上"" return "\"" + bsonValue + "\""; } //其他 return bsonValue.ToString(); }
private MongoNode AddBsonValueNode(string key, BsonValue v, MongoNode parent) { MongoNode node = new MongoNode { Key = key }; if (v.IsBsonNull) { node = AddTreeNode(key, null, parent); } else if (v.IsBoolean) { node = AddTreeNode(key, v.ToBoolean(), parent); } else if (v.IsInt32) { node = AddTreeNode(key, v.ToInt32(), parent); } else if (v.IsInt32 || v.IsInt64) { node = AddTreeNode(key, v.ToInt64(), parent); } else if (v.IsDouble) { node = AddTreeNode(key, v.ToDouble(), parent); } else if (v.IsDecimal128 || v.IsNumeric) { node = AddTreeNode(key, v.ToDecimal(), parent); } else if (v.IsObjectId) { node = AddTreeNode(key, v.AsObjectId, parent); } else if (v.IsString || v.IsGuid) { node = AddTreeNode(key, v.ToString(), parent); } else if (v.IsValidDateTime || v.IsBsonDateTime) { node = AddTreeNode(key, v.ToLocalTime(), parent); } else if (v.IsString) { node = AddTreeNode(key, v.ToString(), parent); } else if (v.IsValidDateTime) { node = AddTreeNode(key, v.ToLocalTime(), parent); } else if (v.IsBsonArray) { var array = v.AsBsonArray; node.Value = $"[{array.Count}]"; node.Type = "Array"; node.ImagePath = ResourcesBase + @"/Images/arr.png"; var i = 0; foreach (var item in array) { AddBsonValueNode($"[{i}]", item, node); i++; } } else { node = AddTreeNode(key, v.ToString(), parent); } return(node); }
private object GetValue(BsonValue value) { if (value.IsString) return value.AsString; else if (value.IsInt32) return value.AsInt32; else if (value.IsInt64) return value.AsInt64; else if (value.IsBoolean) return value.AsBoolean; else if (value.IsDouble) return value.AsDouble; else if (value.IsValidDateTime) return (long)value.ToLocalTime().Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds; //else if (value.IsObjectId) // itemValue = value.AsObjectId.ToString(); return null; }