MongoDB

安装使用

小插曲:项目属性-生成-高级,发现vs2013只支持到C#5.0
C#6.0在vs2015引入,或在vs2013基础上从https://github.com/dotnet/roslyn下载roslync包

MongoDB Client

RoboMongo --> Robo 3T
RoboMongo镜像地址:http://dl.mongodb.org/dl/win32/x86_64
Robo 3T下载地址:https://robomongo.org/download
可分别参考:使用教程1, 使用教程2

.Net MongoDB

驱动下载:https://github.com/mongodb/mongo-csharp-driver/releases,或直接在 nuget.net 中下载包
MongoDB .Net文档:https://api.mongodb.com/csharp/current/html/R_Project_CSharpDriverDocs.htm
版本兼容一览表:MongoDB C#/.NET Driver

基础概念

查询

若类对象T中无_id字段,FindOneAs方法会抛异常:Element '_id' doesnot match any field of class T

新增

  • insert
  • save

insert可以一次性插入一个列表,不用遍历、效率高, save需要遍历列表、一个个插入。
关于insert和save的区别:https://blog.csdn.net/xiaojin21cen/article/details/40480609
注意,文中强调的是自有的主键_id。若插入数据中有自定义为Unique索引的字段,insert和save均会抛异常键重复。

更新

删除

MongoDB资料系列

问题解决

问题1:保存DateTime类型到Mongo后,日期比存入的日期要小
原因:Mongo会将时间保存成UCT时间,即格林威治时间,比北京时间要晚8小时
解决:在时间属性上加标签[BsonDateTimeOptions(Kind = DateTimeKind.Local)]或注册

BsonSerializer.RegisterSerializer(typeof(DateTime),
    new DateTimeSerializer(DateTimeSerializationOptions.LocalInstance));

具体参见:http://www.voidcn.com/article/p-tanzovjm-bsx.html
问题2:在.Net中写enum类型对象到MongoDB,会存储为Int32类型
解决:方法1方法2
问题3:调用Save()方法,提示报错:

No IdGenerator found.  在 MongoDB.Driver.MongoCollection.Save(Type nominalType, Object document, MongoInsertOptions options)
在 MongoDB.Driver.MongoCollection.Save(Type nominalType, Object document)
在 MongoDB.Driver.MongoCollection.Save[TNominalType](TNominalType document)
在 CMB.ScheduleTask.MongoHelper.SaveOne[T](MongoServerSettings connString, String dbName, String collectionName, T entity)

解决:entity中必须要有_id字段。另,MongoDB驱动新版(v2.7.0等)均已去掉Save()方法
问题4:MongoDB(v3.4)连接初始化报错(MongoDB Driver v2.8.1)

Multiple custom attributes of the same type found.    
at System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit)
at System.Runtime.InteropServices.RuntimeInformation.get_FrameworkDescription()  [4.3.0] 
at System.Lazy`1.CreateValue() 
 --- End of stack trace from previous location where exception was thrown ---   
at System.Lazy`1.get_Value()   
at MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateClientDocument(String applicationName)   
at MongoDB.Driver.Core.Connections.BinaryConnectionFactory..ctor(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber)   
at MongoDB.Driver.Core.Configuration.ClusterBuilder.CreateConnectionPoolFactory()   
at MongoDB.Driver.Core.Configuration.ClusterBuilder.CreateServerFactory()   
at MongoDB.Driver.Core.Configuration.ClusterBuilder.CreateClusterFactory()   
at MongoDB.Driver.ClusterRegistry.CreateCluster(ClusterKey clusterKey)   
at MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(ClusterKey clusterKey)  
at MongoDB.Driver.MongoClient..ctor(MongoClientSettings settings)
at Tool.MongoDBHelper`1..ctor(String collectionName)

解决:先降版本至2.7.0。可参考:由System.Runtime.InteropServices.RuntimeInformation.dll引发的MongoDB连接问题

转载于:https://www.cnblogs.com/wjcx-sqh/p/11314615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值