mongoDB连接信息及生成对应的collection生成代码

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:[email protected]:27000";
private static readonly string _connectionString = System.Configuration.ConfigurationSettings.AppSettings["entmongoconectionstring"]; 

//{ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{[email protected]}};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(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", 27017),
//WaitQueueTimeout = new TimeSpan(0, 2, 0),
//
ConnectTimeout = new TimeSpan(0, 0, 30),
};
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);
} 

}
} 
时间: 2024-07-28 12:51:12

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

MongoDB警告信息

MongoDB警告信息: 1. WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2. WARNING: Access control is not enabled for the database. 3. WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 4. WARNING:

MongoDB 连接

你可以使用 MongoDB shell 来连接 MongoDB 服务器.你也可以使用 PHP 来连接 MongoDB.本教程我们会使用 MongoDB shell 来连接 Mongodb 服务,之后的章节我们将会介绍如何通过php 来连接MongoDB服务. 标准 URI 连接语法: mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?option

mongodb连接

MongoDB - 连接 在本教程我们将讨论 MongoDB 的不同连接方式. 启动 MongoDB 服务 在前面的教程中,我们已经讨论了如何启动 MongoDB 服务,你只需要在 MongoDB 安装目录的 bin 目录下执行 mongod 即可. 执行启动操作后,mongodb 在输出一些必要信息后不会输出任何信息,之后就等待连接的建立,当连接被建立后,就会开始打印日志信息. 你可以使用 MongoDB shell 来连接 MongoDB 服务器.你也可以使用 PHP 来连接 MongoDB

利用DBMS_STATS包修改统计信息,欺骗优化器,生成糟糕的执行计划

在使用基于成本的优化器的优化器时,优化器生产执行计划时要估算每条SQL的执行成本,选择最佳的执行计划来执行sql语句.通过操纵统计信息就可以简介操纵执行计划的生成. 当然 需要强调的一点是,这是非常危险的行为 1 创建测试表 SQL> create table test_stats  as   2  select * from dba_objects ; Table created. 2 收集统计信息 SQL> EXEC dbms_stats.gather_table_stats(ownnam

使用Dom4j解析包含有DB连接信息的XML文件以及节点属性的获取

包含DB连接信息的XML文件 1 <!--示例1——三级显示--> 2 <db-connections> 3 <connection> 4 <name>DBTest</name> 5 <jndi></jndi> 6 <url> 7 <![CDATA[jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8]

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

MongoDB连接标准格式: mongodb://[username:[email protected]]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] 参数说明 Mongodb:// 必填的前缀,标识当前字符串为便准链接格式 username:[email protected] 可选项,给出用户名和密码后,在连接数据库服务器后,驱动都会尝试登陆这个数据库 host uri里唯一的必填项,数据库的连

在数据库连接配置文件之前对连接信息进行解密并连接

技术交流群:233513714 1.如果数据库连接密码没有加密的时候连接的配置方式是 <bean id="anteaterDs" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name=&quo

SpringMVC使用隐式jdbc连接信息

本地测试环境中,我们如果使用SpringMVC构建项目时,数据库的"用户名.密码.连接地址"都直接明文写在配置文件中.而一旦项目构建在运行环境的服务器时,对"用户名.密码.连接地址"连接信息进行必要的隐藏,这样更加安全一些,那么如何来做隐式jdbc连接信息呢? 一.隐式jdbc连接信息的可行方案 SpringMVC在构建jdbc连接信息时,一般是在"applicationContext.xml"文件中有如下信息提供给项目的JdbcConnecti

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

执行以下语句可查出用户TJAMIS_LXF连接信息: select schemaname, osuser, process, machine, port, terminal, program from v$session where username = 'TJAMIS_LXF' order by osuser;