創建一個獲取MongoDB數據庫實例的類
public class Db { private static IMongoDatabase db = null; private static readonly object lockHelper = new object(); private Db() { } public static IMongoDatabase GetDb(string connStr, string dbName) { if (db == null) { lock (lockHelper) { if (db == null) { var client = new MongoClient(connStr); db = client.GetDatabase(dbName); } } } return db; } }
創建一個操作MongDB的輔助類
public class MongoDbHelper<T> where T : MongoBaseEntity, new() { private IMongoDatabase db = null; private IMongoCollection<T> collection = null; private readonly IOptions<MongoDBConfig> _options; public MongoDbHelper(IOptions<MongoDBConfig> options) { this._options = options; this.db = Db.GetDb(this._options.Value.ConnectionString, this._options.Value.DbName); this.collection = db.GetCollection<T>(typeof(T).Name); } /// <summary> /// 分頁 /// </summary> /// <param name="filter"></param> /// <param name="sort"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public PagingModel<T> GetPagingData(FilterDefinition<T> filter, SortDefinition<T> sort, int pageIndex, int pageSize) { var list = this.collection .Find(filter) .Sort(sort) .Skip((pageIndex - 1) * pageSize) .Limit(pageSize) .ToList(); var count = this.collection.CountDocuments(filter); var pagingModel = new PagingModel<T>(); pagingModel.Items = list; pagingModel.PageIndex = pageIndex; pagingModel.PageSize = pageSize; pagingModel.TotalRecords = Convert.ToInt32(count); pagingModel.TotalPages = (int)Math.Ceiling((double)count / (double)pageSize); return pagingModel; } }
說明:構造方法public MongoDbHelper(IOptions<MongoDBConfig> options)中的參數,是與配置文件中的MongoDBConfig節點對應的,我的項目是.net core項目。
下面是appsettings.json配置文件部分代碼:
"MongoDBConfig": { "ConnectionString": "mongodb://xxx:xxx@192.168.3.240:27017", "DbName": "xxxDB" }
public class MongoDBConfig { /// <summary> /// 數據庫連接字符串 /// </summary> public string ConnectionString { get; set; } /// <summary> /// 具體數據庫名稱 /// </summary> public string DbName { get; set; } }
創建一個分頁實體類
/// <summary> /// 描述:分頁實體 /// 創建人:蘇本東 /// 創建時間:2019-3-5 19:05:20 /// </summary> /// <typeparam name="T"></typeparam> public class PagingModel<T> where T : class, new() { /// <summary> /// 當前頁碼 /// </summary> public int PageIndex { get; set; } /// <summary> /// 每頁大小 /// </summary> public int PageSize { get; set; } /// <summary> /// 總記錄數 /// </summary> public int TotalRecords { get; set; } /// <summary> /// 總頁數 /// </summary> public int TotalPages { get; set; } /// <summary> /// 每頁數據 /// </summary> public List<T> Items { get; set; } }
最后就是調用了
FilterDefinition<ModbusData> filter = Builders<ModbusData>.Filter.Empty; if (request.Ip.IsNotNullAndEmpty()) { //注意:Eq方法的第一個參數,大小寫需要跟數據庫一致,不然查詢無效 filter = Builders<ModbusData>.Filter.Eq("IP", request.Ip); } var sort = Builders<ModbusData>.Sort.Descending(c => c._id); var pagingModel = this._mongo.GetPagingData(filter, sort, (int)request.page, (int)request.limit);
ModbusData類是與MongoDB的表對應的
提供兩篇文章供學習參考:
https://www.cnblogs.com/quebra/p/10202449.html
https://www.cnblogs.com/woxpp/p/6347429.html
