MongoDB C#samus驱动
MongoDB的c#驱动有两种,官方驱动和samus驱动,不过我更喜欢samus驱动,因为samus驱动提供了丰富的linq操作。
官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads。下载后,还提供了一个酷似msdn的帮助文档。
samus驱动:https://github.com/samus/mongodb-csharp/downloads。
下面是具体代码:
public class MongoDB<T>where T: class,new() { private string connectionString = ConfigurationManager.AppSettings["mongodbConnection"]; private string dataBase= ConfigurationManager.AppSettings["mongodbBase"]; private string collection= ConfigurationManager.AppSettings["mongodbCol"]; private Mongo mongo = null; public MongoDB(string connectionStr,string dbBase,string col) { if (connectionStr != "") { connectionString = connectionStr; } if (dbBase != "") dataBase = dbBase; if (col != "") collection = col; } /// <summary> /// 实现linq查询的映射配置 /// </summary> public MongoConfiguration configuration { get { var config = new MongoConfigurationBuilder(); config.Mapping(mapping => { mapping.DefaultProfile(profile => { profile.SubClassesAre(t => t.IsSubclassOf(typeof(T))); mapping.Map<T>(); }); }); config.ConnectionString(connectionString); return config.BuildConfiguration(); } } //得到collecaton public IMongoCollection<T> GetMongoDBCollction() { mongo = new Mongo(configuration); try { mongo.Connect(); var db = mongo.GetDatabase(dataBase); return db.GetCollection<T>(collection); } catch (Exception ex) { throw; } } /// <summary> /// 添加 /// </summary> /// <param name="t"></param> public void Insert(T t) { try { var col= GetMongoDBCollction(); col.Insert(t, true); } catch (Exception ex) { throw; } finally { mongo.Disconnect(); } } /// <summary> /// 更新 /// </summary> /// <param name="t"></param> /// <param name="func">where的lambda条件</param> public void Update(T t, Expression<Func<T, bool>> func) { try { var col = GetMongoDBCollction(); col.Update<T>(t, func, true); } catch (Exception ex) { throw; } finally { mongo.Disconnect(); } } /// <summary> /// 分页 /// </summary> /// <param name="pageSize">每页多少条</param> /// <param name="pageIndex">当前页</param> /// <param name="func">where的lambda条件</param> /// <param name="pageCount">总共多少条</param> /// <returns></returns> public List<T> GetPages<TKey>(int pageSize, int pageIndex, Expression<Func<T, bool>> func, Expression<Func<T, TKey>> orderBy, out int pageCount) { pageCount = 0; try { var col = GetMongoDBCollction(); List<T> list = null; if (func != null) { pageCount = col.Linq().Where(func).ToList().Count; if (orderBy != null) { list = col.Linq().Where(func).OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } else { list = col.Linq().Where(func).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } } else { pageCount = col.Linq().ToList().Count; if (orderBy != null) { list = col.Linq().OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } else { list = col.Linq().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } } return list; } catch (Exception ex) { throw; } finally { mongo.Disconnect(); } } //linq 分页 public List<T> GetLinqPages<TKey>(int pageSize, int pageIndex, Expression<Func<T, bool>> func, Expression<Func<T, TKey>> orderBy, out int pageCount) { pageCount = 0; try { var col = GetMongoDBCollction(); List<T> list = null; var query = from a in col.Linq() select a; if (func!=null) { query=query.Where(func); } if (orderBy != null) { query = query.OrderByDescending(orderBy); } pageCount = query.ToList().Count; list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return list; } catch { throw; } finally { mongo.Disconnect(); } } /// <summary> /// 读取一条数据 /// </summary> /// <param name="func"></param> /// <returns></returns> public T GetSingle(Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo(configuration)) { try { var col = GetMongoDBCollction(); return col.FindOne(func); } catch (Exception e) { throw; } finally { mongo.Disconnect(); } } } public void Delete(Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo(configuration)) { try { var col = GetMongoDBCollction(); //这个地方要注意,一定要加上T参数,否则会当作object类型处理 //导致删除失败 col.Remove<T>(func); } catch (Exception e) { throw; } finally { mongo.Disconnect(); } } } /// <summary> /// 得到总数量 /// </summary> /// <param name="func"></param> /// <returns></returns> public int GetCount(Expression<Func<T, bool>> func) { using (Mongo mongo = new Mongo(configuration)) { try { var col = GetMongoDBCollction(); return col.Linq().Where(func).ToList().Count; } catch (Exception e) { throw; } finally { mongo.Disconnect(); } } } }