C#操作MongoDB帮助类
利用MongoDB for C# Driver编写访问MongoDB数据库的帮助类。
1、创建数据库连接
/// <summary> /// 获取数据库实例对象 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <returns>数据库实例对象</returns> private static MongoDatabase GetDatabase(string connectionString, string dbName) { var server = MongoServer.Create(connectionString); return server.GetDatabase(dbName); }
2、新增数据
/// <summary> /// 插入一条记录 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据名称</param> /// <param name="collectionName">集合名称</param> /// <param name="model">数据对象</param> public static void Insert<T>(string connectionString, string dbName, string collectionName, T model) where T : EntityBase { if (model == null) { throw new ArgumentNullException("model", "待插入数据不能为空"); } var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection<T>(collectionName); collection.Insert(model); }
3、更新数据
/// <summary> /// 更新数据 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <param name="dictUpdate">更新字段</param> public static void Update(string connectionString, string dbName, string collectionName, IMongoQuery query, Dictionary<string, BsonValue> dictUpdate) { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection(collectionName); var update = new UpdateBuilder(); if (dictUpdate != null && dictUpdate.Count > 0) { foreach (var item in dictUpdate) { update.Set(item.Key, item.Value); } } collection.Update(query, update, UpdateFlags.Multi); } /// <summary> /// 更新数据 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <param name="update">更新条件</param> public static void Update(string connectionString, string dbName, string collectionName, IMongoQuery query, IMongoUpdate update) { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection(collectionName); collection.Update(query, update, UpdateFlags.Multi); }
4、查询数据
/// <summary> /// 根据ID获取数据对象 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="id">ID</param> /// <returns>数据对象</returns> public static T GetById<T>(string connectionString, string dbName, string collectionName, string id) where T : EntityBase { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection<T>(collectionName); var tId = new ObjectId(id); return collection.FindOneById(tId); } /// <summary> /// 根据查询条件获取一条数据 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <returns>数据对象</returns> public static T GetOneByCondition<T>(string connectionString, string dbName, string collectionName, IMongoQuery query) where T : EntityBase { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection<T>(collectionName); return collection.FindOne(query); } /// <summary> /// 根据查询条件获取多条数据 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <returns>数据对象集合</returns> public static List<T> GetManyByCondition<T>(string connectionString, string dbName, string collectionName, IMongoQuery query) where T : EntityBase { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection<T>(collectionName); return collection.Find(query).ToList(); } /// <summary> /// 根据查询条件获取多条数据(分页) /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <param name="sortBy">排序</param> /// <param name="pageIndex">页号</param> /// <param name="pageSize">每页大小</param> /// <returns>分页数据集合</returns> public static PagingList<T> GetManyByPageCondition<T>(string connectionString, string dbName, string collectionName, IMongoQuery query, IMongoSortBy sortBy, int pageIndex, int pageSize) where T : EntityBase { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection<T>(collectionName); var result = new PagingList<T>(); result.Total = (int)collection.Count(query); result.CurrentPageList = collection.Find(query).SetSortOrder(sortBy).SetSkip((pageIndex - 1)*pageSize).SetLimit(pageSize).ToList(); return result; } /// <summary> /// 获取集合中的所有数据 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <returns>数据对象集合</returns> public static List<T> GetAll<T>(string connectionString, string dbName, string collectionName) where T : EntityBase { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection<T>(collectionName); return collection.FindAll().ToList(); }
5、删除数据
/// <summary> /// 删除集合中符合条件的数据 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> public static void DeleteByCondition(string connectionString, string dbName, string collectionName, IMongoQuery query) { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection(collectionName); collection.Remove(query); } /// <summary> /// 删除集合中的所有数据 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <param name="dbName">数据库名称</param> /// <param name="collectionName">集合名称</param> public static void DeleteAll(string connectionString, string dbName, string collectionName) { var db = GetDatabase(connectionString, dbName); var collection = db.GetCollection(collectionName); collection.RemoveAll(); }