.net,个人封装MONGODDB的操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver; namespace CDPWIB.DAL
{
public class MongoConnectionFactory
{
//private static readonly string _connectionString = "mongodb://username:pwd@localhost:27000";
private static readonly string _connectionString = System.Configuration.ConfigurationSettings.AppSettings["entmongoconectionstring"]; //{ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{DBNAME@USERNAME}};GuidRepresentation=CSharpLegacy;IPv6=False;MaxConnectionIdleTime=00:10:00;MaxConnectionLifeTime=00:30:00;MaxConnectionPoolSize=100;MinConnectionPoolSize=0;ReadPreference=Primary;ReplicaSetName=;SecondaryAcceptableLatency=00:00:00.0150000;Servers=10.10.10.252:27000;SocketTimeout=00:00:00;Ssl=False;SslVerifyCertificate=True;WaitQueueSize=500;WaitQueueTimeout=00:02:00WriteConcern=w=1;}
//public static MongoClientSettings clintser = new MongoClientSettings
//{
// //Credentials = new List<MongoCredential>
// //{
// // MongoCredential.CreateMongoCRCredential("USERNAME", "DBNAME", "DB")
// //},
// MaxConnectionLifeTime = new TimeSpan(0, 5, 0),
// MaxConnectionIdleTime = new TimeSpan(0, 2, 0),
// MinConnectionPoolSize = 0,
// MaxConnectionPoolSize = 1000,
// SocketTimeout = new TimeSpan(0, 2, 0),
// WaitQueueTimeout = new TimeSpan(0, 0, 30), // //Server = new MongoServerAddress("10.10.10.252", 27000),
// Server = new MongoServerAddress("127.0.0.1", 27000),
// //WaitQueueTimeout = new TimeSpan(0, 2, 0),
// //
// ConnectTimeout = new TimeSpan(0, 0, 30),
//}; public static MongoClientSettings clintser = new MongoClientSettings
{
Credentials = new List<MongoCredential>
{
//MongoCredential.CreateMongoCRCredential("usename", "db", "pwd")
},
MaxConnectionLifeTime = new TimeSpan(, , ),
MaxConnectionIdleTime = new TimeSpan(, , ),
MinConnectionPoolSize = ,
MaxConnectionPoolSize = ,
SocketTimeout = new TimeSpan(, , ),
WaitQueueTimeout = new TimeSpan(, , ), //Server = new MongoServerAddress("10.10.10.252", 27000),
Server = new MongoServerAddress("127.0.0.1", ),
//WaitQueueTimeout = new TimeSpan(0, 2, 0),
//
ConnectTimeout = new TimeSpan(, , ),
};
private static MongoClient mongoClient = new MongoClient(clintser); private static MongoServer mongoServer = mongoClient.GetServer(); public static MongoClient GetMongoClient()
{
return mongoClient;
} public static MongoServer GetMongoServer()
{
//var seting = mongoServer.Settings;
//seting.ConnectTimeout = new TimeSpan(0, 2, 0);
//seting.MinConnectionPoolSize = 100;
//seting.MaxConnectionPoolSize = 1000;
//seting.SocketTimeout = new TimeSpan(0, 2, 0);
//seting.WaitQueueTimeout = new TimeSpan(0, 2, 0); return mongoServer;
} public static MongoDatabase GetMongoDataBase(string dbname)
{ return GetMongoServer().GetDatabase(dbname);
} public static MongoCollection<T> GetMongoCollction<T>(MongoDatabase database, string collectionname)
{ return database.GetCollection<T>(collectionname);
}
public static MongoCollection<T> GetMongoCollction<T>(string databasename, string collectionname)
{
MongoDatabase database = GetMongoDataBase(databasename);
return database.GetCollection<T>(collectionname);
} public static MongoCollection<T> GetMongoCollction<T>(string databasename)
{
MongoDatabase database = GetMongoDataBase(databasename);
return database.GetCollection<T>(typeof(T).Name);
} public static MongoCollection GetMongoCollction(string databasename, string collectionname)
{
MongoDatabase database = GetMongoDataBase(databasename);
return database.GetCollection(collectionname);
} public static MongoCollection<T> GetMongoCollction<T>(MongoDatabase database, Type type)
{
return database.GetCollection<T>(type.Name);
}
public static MongoCollection<T> GetMongoCollction<T>(string databasename, Type type)
{
MongoDatabase database = mongoServer.GetDatabase(databasename);
return database.GetCollection<T>(type.Name);
} }
}

最新文章

  1. Baseadapter与Simpleadapter之争
  2. CSS 如何使DIV层水平居中
  3. centos 解压jdk安装包方式安装jdk 出现 java/lang/NoClassDefFoundError: java/lang/Object 错误
  4. Yocto开发笔记之《驱动调试-GPS数据采集》(QQ交流群:519230208)
  5. CSS Image Sprite--网页图片应用处理方式
  6. 使用Angular和Nodejs搭建聊天室
  7. IDHttp的基本用法(转)
  8. NFC(11)MifareUltralight格式规范及读写示例
  9. php正则的使用[替换,匹配]
  10. HTML中直接写js 函数
  11. MyBatis map foreach
  12. 自定义修改Anaconda Jupyterlab Home目录
  13. 你循环的时候就可以给他们赋值了,那么就不用addClass,再根据类选择器处理,代码能一气呵成就别写成两段了
  14. kmeans
  15. 20165223 学习基础和C语言基础调查
  16. 开源医学图像处理平台NiftyNet介绍
  17. Oracle学习——第一章
  18. (一)什么是webservice?
  19. mui init 出现无法引入子页面问题
  20. BZOJ3514:GERALD07加强版(LCT,主席树)

热门文章

  1. Linux学习(二) --- 常用命令
  2. 论文或github中一些通用思想
  3. upstream(负载均衡)
  4. idea快捷键(最常用)
  5. SVN一直清理解决
  6. Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP)
  7. jenkins_2
  8. Lamda过滤替换List元素的属性值
  9. logstash nested内嵌字段 field protobuf解码 codec 的解决办法
  10. okhttp 拦截问题