码迷,mamicode.com
首页 > 数据库 > 详细

MangoDB在C#中的使用

时间:2019-12-06 16:26:36      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:设置   http   特性   官方文档   ISE   soft   tls   with   blog   

http://blog.sina.com.cn/s/blog_927f3c2401011937.html 图形工具

http://api.mongodb.org/csharp/current/html/R_Project_CSharpDriverDocs.htm C#API

http://docs.mongodb.org/manual/reference/method/  Shell命令

http://docs.mongodb.org/manual/ MongoDB官方文档

http://blog.michaelckennedy.net/2013/04/08/optimistic-concurrency-in-mongodb-using-net-and-csharp/ MongoDB.Kennedy

https://mongodb-documentation.readthedocs.org/en/latest/ecosystem/tutorial/serialize-documents-with-the-csharp-driver.html   Serialize Documents with the CSharp Driver(使用c#对实体进行序列化)

https://www.mongodb.com/thank-you/download/mongodb-enterprise 官网下载地址

http://www.ttlsa.com/mms/follow-me-to-use-mongodb-mms-services/ 随我来使用mongodb mms服务

当类体结构改变,删除了某个属性,或者某个属性的类型发生了改变?

1、属性的类型发生了改变

技术图片
  1 public class Address { public ObjectId Id; public string ZipCode; }
  2 
  3 public class ZipCodeSerializer : BsonBaseSerializer { public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.String: return bsonReader.ReadString(); case BsonType.Int32: return bsonReader.ReadInt32().ToString(); default: var message = string.Format("ZipCodeSerializer expects to find a String or an Int32, not a {0}.", bsonType); throw new BsonSerializationException(message); } } public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { if (value == null) { bsonWriter.WriteNull(); } else { bsonWriter.WriteString((string)value); } } }
  4 
  5 BsonClassMap.RegisterClassMap<Address>(cm => { cm.AutoMap(); cm.GetMemberMap(a => a.ZipCode).SetSerializer(new ZipCodeSerializer()); });
  6 
View Code

2、删除了某个属性

技术图片
  1 var allDocuments = collectionToUpdate .FindAll();foreach(var document in allDocuments ){var oldFieldValue = document ["OldFieldName"];if(!document.Contains("NewFieldName")) document.Add("NewFieldName", oldFieldValue); document.Remove("OldFieldName"); collectionToUpdate.Save(document);}
  2 
  3 3、使用newtonsoft.json转换为json
  4 
  5 class ObjectId Converter:JsonConverter{publicoverridevoidWriteJson(JsonWriter writer,object value,JsonSerializer serializer){ serializer.Serialize(writer, value.ToString());}publicoverrideobjectReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer){thrownewNotImplementedException();}publicoverride bool CanConvert(Type objectType){returntypeof(ObjectId).IsAssignableFrom(objectType);//return true;}}
  6 
  7         public class ObjectIdConverter : TypeConverter
  8 
  9         {
 10 
 11             public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 12 
 13             {
 14 
 15                 if (sourceType == typeof(string))
 16 
 17                 {
 18 
 19                     return true;
 20 
 21                 }
 22 
 23                 return base.CanConvertFrom(context, sourceType);
 24 
 25             }
 26 
 27             public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 28 
 29             {
 30 
 31                 if (value is string)
 32 
 33                     return ObjectId.Parse((string)value);
 34 
 35                 return base.ConvertFrom(context, culture, value);
 36 
 37             }
 38 
 39             // Overrides the ConvertTo method of TypeConverter.
 40 
 41             public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 42 
 43             {
 44 
 45                 if (destinationType == typeof(string))
 46 
 47                 {
 48 
 49                     return ((ObjectId)value).ToString();
 50 
 51                 }
 52 
 53                 return base.ConvertTo(context, culture, value, destinationType);
 54 
 55             }
 56 
 57         }
 58 
View Code

使用:[TypeConverter(typeof(ObjectIdConverter))]

4、实体中排除某些字段,不写入到集合中

publicclassMyClass{ [BsonIgnore]publicstringSomeProperty{get;set;}}

或者使用初始化代码的形式,设置映射关系

BsonClassMap.RegisterClassMap<MyClass>(cm=>{cm.AutoMap();cm.UnmapProperty(c=>c.SomeProperty);});

5、在连接参数中设置用户名和密码

http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-csharp-driver/

6、mongodb中时间为当前小时数 减 8

分       析:存储在mongodb中的时间是标准时间UTC +0:00 而咱们中国的失去是+8.00 。

解决方法:在datetime属性上增加特性

[BsonDateTimeOptions(Kind = DateTimeKind.Local)]

public DateTime Birth{get;set;}

MangoDB在C#中的使用

标签:设置   http   特性   官方文档   ISE   soft   tls   with   blog   

原文地址:https://www.cnblogs.com/smallidea/p/11995955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!