MongoDB的官方C#驱动可以通过这个链接得到。链接提供了.msi和.zip两种方式获取驱动dll文件。C#驱动的基本数据库连接,增删改查操作。在使用C#驱动的时候,要在工程中添加"MongoDB.Bson.dll"和"MongoDB.Driver.dll"的引用。同时要在代码中加入下面两个using语句。
- k# S/ |2 a& X8 P+ Q$ u1 c
- using MongoDB.Bson.Serialization.Conventions;
- using MongoDB.Driver;
- using MongoDB.Driver.Linq;
- using System.Text;
' J& C! @9 T9 t' v: n% C4 H; O4 t$ Y
注册常用约定9 v' H1 K9 b8 F( N O
- public static void RigisterConventions()
- {
- var pack = new ConventionPack();
- //元素名称序列化成驼峰形式
- pack.Add(new CamelCaseElementNameConvention());
- ConventionRegistry.Register("MyConventions", pack, x => true);
- }
3 ?! |# I9 D4 ]5 \( u
初始化Mongo帮助类( M9 C, u! T9 \$ b( k% p# C% n
- /// <summary>
- /// 初始化Mongo帮助类
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="dbName">数据库名</param>
- public MongoHelper(string connectionString, string dbName)
- {
- _client = new MongoClient(connectionString);
- _database = _client.GetDatabase(dbName);
- }
. ^9 |& I. i# `( F, Q" \" P
获取集合# ^1 h# _8 u- t0 q
- /// <summary>
- /// 获取集合
- /// </summary>
- /// <typeparam name="T">集合类型</typeparam>
- /// <param name="collectionName">集合名</param>
- /// <returns></returns>
- public IMongoCollection<T> GetCollection<T>(string collectionName)
- {
- return _database.GetCollection<T>(collectionName);
- }
, M" C$ i7 a2 h+ ]
完整代码
1 x8 k4 _5 f V1 Q3 v- /// <summary>
- /// MongoDB访问帮助类
- /// </summary>
- public class MongoHelper
- {
- private MongoClient _client;
- private IMongoDatabase _database;
-
- /// <summary>
- /// 注册常用约定
- /// </summary>
- public static void RigisterConventions()
- {
- var pack = new ConventionPack();
- //元素名称序列化成驼峰形式
- pack.Add(new CamelCaseElementNameConvention());
- ConventionRegistry.Register("MyConventions", pack, x => true);
- }
-
- /// <summary>
- /// 初始化Mongo帮助类
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="dbName">数据库名</param>
- public MongoHelper(string connectionString, string dbName)
- {
- _client = new MongoClient(connectionString);
- _database = _client.GetDatabase(dbName);
- }
-
- /// <summary>
- /// 数据库
- /// </summary>
- public IMongoDatabase Db => _database;
-
- /// <summary>
- /// 获取集合
- /// </summary>
- /// <typeparam name="T">集合类型</typeparam>
- /// <param name="collectionName">集合名</param>
- /// <returns></returns>
- public IMongoCollection<T> GetCollection<T>(string collectionName)
- {
- return _database.GetCollection<T>(collectionName);
- }
-
- /// <summary>
- /// 获取Queryable对象
- /// </summary>
- /// <typeparam name="T">对象类型</typeparam>
- /// <param name="collectionName">集合名</param>
- /// <returns></returns>
- public IMongoQueryable<T> GetQueryable<T>(string collectionName)
- {
- return GetCollection<T>(collectionName).AsQueryable();
- }
-
- /// <summary>
- /// 模糊查询转换特殊字符:正则表达式有以下特殊字符。需要转义 * . ? + $ ^ [ ] ( ) { } | \ /
- /// 如:{"phone":/U9G\/XoDNo8ozbwbxal\+Qzg==/}
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string ChangeSpecialCharacter(string str)
- {
- if (string.IsNullOrEmpty(str))
- return str;
- StringBuilder retValue = new StringBuilder();
- string str1 = "*.?+$^[](){}|\\/";
- for (int i = 0; i < str.Length; i++)
- {
- string ss = str[i].ToString();
- if (str1.Contains(ss))
- {
- ss = "\" + ss;
- }
- retValue.Append(ss);
- }
- return retValue.ToString();
- }
5 h! @+ l- |# _! u& v |