安装使用
小插曲
:项目属性-生成-高级,发现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
- 类对象T指定属性[BsonIgnoreExtraElements]
- 类对象T使用BsonDocument
为所有类实现BsonIgnoreExtraElements,具体参见:Implement for all classes BsonIgnoreExtraElements
新增
- insert
- save
insert可以一次性插入一个列表,不用遍历、效率高, save需要遍历列表、一个个插入。
关于insert和save的区别:https://blog.csdn.net/xiaojin21cen/article/details/40480609
注意,文中强调的是自有的主键_id。若插入数据中有自定义为Unique索引的字段,insert和save均会抛异常键重复。
更新
删除
问题解决
问题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连接问题