定义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;
}
定义MongoRepositoryBase类,并实现IMongoRepositoryBase接口
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;
}
}
}