定义IMongoRepositoryBase接口
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
public
interface IMongoRepositoryBase
{
/// <summary>
/// 新增一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
void Insert<T>(T model) where T : class;
/// <summary>
/// 批量新增数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="list"> 泛型集合 </param>
void Insert<T>(IList<T> list) where T : class;
/// <summary>
/// 更新一条数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="model"> 实体类 </param>
/// <param name="where"> 查询条件 </param>
bool Update<T>(T model, Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 删除数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
bool Delete<T>(Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 根据条件,获取一条记录
/// </summary>
/// <typeparam name="T"> 返回值类型 </typeparam>
/// <param name="where"></param>
/// <returns></returns>
T GetModel<T>(Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
IList<T> GetList<T>(Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="orderBy"></param>
/// <returns></returns>
IList<T> GetList<T>(Expression<Func<T, object>> orderBy) where T : class;
/// <summary>
/// 获取带排序的列表
/// </summary>
/// <typeparam name="T"> 数据类型 </typeparam>
/// <param name="where"> 查询条件 </param>
/// <param name="orderBy"> 排序 </param>
/// <returns></returns>
IList<T> GetList<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy) where T : class;
/// <summary>
/// 获取分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="where"></param>
/// <param name="orderby"></param>
/// <returns></returns>
IList<T> GetList<T>( int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> orderby) where T : class;
/// <summary>
/// 获取总记录数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
long GetTotalCount<T>(Expression<Func<T, bool>> where) where T : class;
}
{
/// <summary>
/// 新增一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
void Insert<T>(T model) where T : class;
/// <summary>
/// 批量新增数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="list"> 泛型集合 </param>
void Insert<T>(IList<T> list) where T : class;
/// <summary>
/// 更新一条数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="model"> 实体类 </param>
/// <param name="where"> 查询条件 </param>
bool Update<T>(T model, Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 删除数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
bool Delete<T>(Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 根据条件,获取一条记录
/// </summary>
/// <typeparam name="T"> 返回值类型 </typeparam>
/// <param name="where"></param>
/// <returns></returns>
T GetModel<T>(Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
IList<T> GetList<T>(Expression<Func<T, bool>> where) where T : class;
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="orderBy"></param>
/// <returns></returns>
IList<T> GetList<T>(Expression<Func<T, object>> orderBy) where T : class;
/// <summary>
/// 获取带排序的列表
/// </summary>
/// <typeparam name="T"> 数据类型 </typeparam>
/// <param name="where"> 查询条件 </param>
/// <param name="orderBy"> 排序 </param>
/// <returns></returns>
IList<T> GetList<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy) where T : class;
/// <summary>
/// 获取分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="where"></param>
/// <param name="orderby"></param>
/// <returns></returns>
IList<T> GetList<T>( int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> orderby) where T : class;
/// <summary>
/// 获取总记录数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
long GetTotalCount<T>(Expression<Func<T, bool>> where) where T : class;
}
定义MongoRepositoryBase类,并实现IMongoRepositoryBase接口
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
public
class MongoRepositoryBase : IMongoRepositoryBase
{
/// <summary>
/// 数据库
/// </summary>
private IMongoDatabase db { get; set; }
/// <summary>
/// 表名
/// </summary>
private string collectionName { get; set; }
public MongoRepositoryBase( string collectionName)
{
this.db = MongoDBConnection.GetMongoDatabase();
this.collectionName = collectionName;
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
public void Insert<T>(T model) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
// 向链表中批量写入数据
collection.InsertOneAsync(model);
}
/// <summary>
/// 批量新增数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="list"> 泛型集合 </param>
/// <param name="collectionName"> 表名 </param>
public void Insert<T>(IList<T> list) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
// 向链表中批量写入数据
collection.InsertManyAsync(list);
}
/// <summary>
/// 更新一条数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="model"> 实体类 </param>
/// <param name="collectionName"> 表名 </param>
/// <param name="where"> 查询条件 </param>
public bool Update<T>(T model, Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
// 要更新的字段集合
var fieldList = new List<UpdateDefinition<T>>();
foreach ( var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (property.Name != " Id ") // 更新集中不能有实体键_id
{
fieldList.Add(Builders<T>.Update.Set(property.Name, property.GetValue(model)));
}
}
return collection.UpdateOneAsync( where, Builders<T>.Update.Combine(fieldList)).Result.ModifiedCount > 0 ? true : false;
}
public bool Delete<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.DeleteOneAsync( where).Result.DeletedCount > 0 ? true : false;
}
/// <summary>
/// 根据条件,获取一条记录
/// </summary>
/// <typeparam name="T"> 返回值类型 </typeparam>
/// <param name="collectionName"> 表名 </param>
/// <param name="where"></param>
/// <returns></returns>
public T GetModel<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( where).FirstOrDefaultAsync().Result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetList<T>() where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( new BsonDocument()).ToListAsync().Result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="orderBy"></param>
/// <returns></returns>
public IList<T> GetList<T>(Expression<Func<T, object>> orderBy) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( new BsonDocument()).SortByDescending(orderBy).ToListAsync().Result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
public IList<T> GetList<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( where).ToListAsync().Result;
}
/// <summary>
/// 获取带排序的列表
/// </summary>
/// <typeparam name="T"> 数据类型 </typeparam>
/// <param name="where"> 查询条件 </param>
/// <param name="orderBy"> 排序 </param>
/// <returns></returns>
public IList<T> GetList<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( where).SortByDescending(orderBy).ToListAsync().Result;
}
/// <summary>
/// 获取分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="where"></param>
/// <param name="orderby"></param>
/// <returns></returns>
public IList<T> GetList<T>( int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> orderby) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
var skip = (pageIndex - 1) * pageSize;
return collection.Find( where).SortByDescending( orderby).Skip(skip).Limit(pageSize).ToListAsync().Result;
}
/// <summary>
/// 获取总记录数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
public long GetTotalCount<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
if ( where != null)
{
return collection.CountAsync( where).Result;
}
else
{
return collection.CountAsync( new BsonDocument()).Result;
}
}
}
{
/// <summary>
/// 数据库
/// </summary>
private IMongoDatabase db { get; set; }
/// <summary>
/// 表名
/// </summary>
private string collectionName { get; set; }
public MongoRepositoryBase( string collectionName)
{
this.db = MongoDBConnection.GetMongoDatabase();
this.collectionName = collectionName;
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
public void Insert<T>(T model) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
// 向链表中批量写入数据
collection.InsertOneAsync(model);
}
/// <summary>
/// 批量新增数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="list"> 泛型集合 </param>
/// <param name="collectionName"> 表名 </param>
public void Insert<T>(IList<T> list) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
// 向链表中批量写入数据
collection.InsertManyAsync(list);
}
/// <summary>
/// 更新一条数据
/// </summary>
/// <typeparam name="T"> 泛型 </typeparam>
/// <param name="model"> 实体类 </param>
/// <param name="collectionName"> 表名 </param>
/// <param name="where"> 查询条件 </param>
public bool Update<T>(T model, Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
// 要更新的字段集合
var fieldList = new List<UpdateDefinition<T>>();
foreach ( var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (property.Name != " Id ") // 更新集中不能有实体键_id
{
fieldList.Add(Builders<T>.Update.Set(property.Name, property.GetValue(model)));
}
}
return collection.UpdateOneAsync( where, Builders<T>.Update.Combine(fieldList)).Result.ModifiedCount > 0 ? true : false;
}
public bool Delete<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.DeleteOneAsync( where).Result.DeletedCount > 0 ? true : false;
}
/// <summary>
/// 根据条件,获取一条记录
/// </summary>
/// <typeparam name="T"> 返回值类型 </typeparam>
/// <param name="collectionName"> 表名 </param>
/// <param name="where"></param>
/// <returns></returns>
public T GetModel<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( where).FirstOrDefaultAsync().Result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetList<T>() where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( new BsonDocument()).ToListAsync().Result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="orderBy"></param>
/// <returns></returns>
public IList<T> GetList<T>(Expression<Func<T, object>> orderBy) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( new BsonDocument()).SortByDescending(orderBy).ToListAsync().Result;
}
/// <summary>
/// 获取列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
public IList<T> GetList<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( where).ToListAsync().Result;
}
/// <summary>
/// 获取带排序的列表
/// </summary>
/// <typeparam name="T"> 数据类型 </typeparam>
/// <param name="where"> 查询条件 </param>
/// <param name="orderBy"> 排序 </param>
/// <returns></returns>
public IList<T> GetList<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
return collection.Find( where).SortByDescending(orderBy).ToListAsync().Result;
}
/// <summary>
/// 获取分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="where"></param>
/// <param name="orderby"></param>
/// <returns></returns>
public IList<T> GetList<T>( int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> orderby) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
var skip = (pageIndex - 1) * pageSize;
return collection.Find( where).SortByDescending( orderby).Skip(skip).Limit(pageSize).ToListAsync().Result;
}
/// <summary>
/// 获取总记录数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="where"></param>
/// <returns></returns>
public long GetTotalCount<T>(Expression<Func<T, bool>> where) where T : class
{
// 获取链表
var collection = db.GetCollection<T>(collectionName);
if ( where != null)
{
return collection.CountAsync( where).Result;
}
else
{
return collection.CountAsync( new BsonDocument()).Result;
}
}
}