.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);
} }
}

mongoDB连接信息及生成对应的collection生成代码的更多相关文章

  1. MongoDB警告信息

    更多内容推荐微信公众号,欢迎关注: MongoDB警告信息: 1. WARNING: Using the XFS filesystem is strongly recommended with the ...

  2. PHP - MongoDB连接攻略

    http://blog.163.com/lgh_2002/blog/static/44017526201261111044608/ 使用PHP的Mongo扩展连接MongoDB.通过new Mongo ...

  3. MongoDB 连接

    启动 MongoDB服务 在前面的教程中,我们已经讨论了如何启动MongoDB服务,你只需要在MongoDB安装目录的bin目录下执行'mongod'即可. 执行启动操作后,mongodb在输出一些必 ...

  4. MongoDB学习笔记——MongoDB 连接配置

    MongoDB连接标准格式: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[dat ...

  5. 查看Oracle数据库某用户的连接信息

    执行以下语句可查出用户TJAMIS_LXF连接信息: select schemaname, osuser, process, machine, port, terminal, program from ...

  6. cat .git/config查看远端服务器信息(git的配置信息:远端服务器连接信息)

    本地git库中,查找其连接的远端服务器信息: 每个git库都会有一个配置信息文件.git/config. cat .git/config,可以看到信息如下: [core]         reposi ...

  7. cdnbest如何查看节点和站点的流量,负载和连接信息

    1. 通过查看top信息,查看该区域下所有节点和有访问量的站点的负载情况 点节点列表==>top图标 2. 查看单台节点的负载和连接信息 点节点列表==>管理 点击下图中三个红框可以查看单 ...

  8. [Oracle]如何获得出现故障时,客户端的详细连接信息

    [Oracle]如何获得出现故障时,客户端的详细连接信息 客户坚持说 只是在 每天早上5点才运行下面的语句: select / * + FULL (TAB001_TT01) * / 'TAB001_T ...

  9. AIX 5335端口IBM WebSphere应用服务器关闭连接信息泄露漏洞的修复

    今天按要求协助进行漏洞修复,有个“IBM WebSphere应用服务器关闭连接信息泄露漏洞”,一直没太搞清是不是没打补丁引起的问题. 感觉同样的安装有的报这漏洞有的不报,而报的有的是应用端口,有时是控 ...

随机推荐

  1. Pyspider的简单介绍和初使用

    Pyspider   Pyspider是由国人(binux)编写的强大的网络爬虫系统 Ptspider带有强大的WebUi / 脚本编辑器 / 任务监控器 / 项目管理器以及结果处理器.他支持多种数据 ...

  2. Chladni Figure CodeForces - 1162D (暴力,真香啊~)

    Chladni Figure CodeForces - 1162D Inaka has a disc, the circumference of which is nn units. The circ ...

  3. Python列出文件夹中的文件

    几乎所有的关于操作系统的内容可以在python 官方文档中找到:https://docs.python.org/3/library/os.html#module-os 其中os.path被单独列出:h ...

  4. 独立t检验

    方差相同个数相同的独立t检验 5.某饲料厂要比较A.B两种配合饲料在养猪生产中的效果,选取12头情况相似的猪,随机分成两组,分别饲喂两种配合饲料,其60天增重(单位:kg)见下表. 饲 料 60d增重 ...

  5. 踏出第一步——安装并跑通python程序

    一.首先学会安装python软件 1.在浏览器下输入安装python软件的官方网址. 点击打开链接 2.在界面上点击"Downloads"下的"Windows" ...

  6. eureka学习之二:自我保护机制

    提供者和消费者:消费者通过注册服务名称,找rpc远程地址,调用提供者的接口 Eureka的自我保护机制:

  7. Java基础篇 - 强引用、弱引用、软引用和虚引用

    Java基础篇 - 强引用.弱引用.软引用和虚引用 原创零壹技术栈 最后发布于2018-09-09 08:58:21 阅读数 4936 收藏展开前言Java执行GC判断对象是否存活有两种方式其中一种是 ...

  8. Unable to cast object of type 'System.String' to type 'System.Int32'.

    原因 数据库中 code 字段 类型为 varchar 而实体的类型为 int  导致string 类型无法转化为int 类型而报错 public int code { get; set; } 参考: ...

  9. C/C++中开平方函数sqrt()的用法

    开平方使用sqrt()函数 使用方法: 包含于math.h头文件 sqrt(float * number),返回number的开平方数,返回值为浮点型 sqrt使用时大多需要要强制类型转化,因为sqr ...

  10. 关于TensorFlow2的tf.function()和AutoGraph的一些问题解决

    在使用TensorFlow的AutoGraph的时候出现了一些问题,特此记录 AutoGraph did not convert this function. Try decorating it di ...