是這樣的,我們有一個場景,另一個服務器是寫到MongoDB里面,我們的MVC頁面要展示,需要分頁展示
自己寫了一個DAL
public class MongoConnect{public string ConnectString { get; set; }}public class MongoBaseDAL<TEntity>{public MongoBaseDAL(IOptions<MongoConnect> options){ConnectString = options.Value.ConnectString;}private string ConnectString { get; set; } = "192.168.50.110:27017";protected MongoClient Create(){var client = new MongoClient($"mongodb://{ConnectString}");return client;}protected IMongoDatabase GetDatabase(string database){var client = Create();var db = client.GetDatabase(database);return db;}protected IMongoCollection<TEntity> CreateQuery(string database,string tableName){var db = GetDatabase(database);return db.GetCollection<TEntity>(tableName);}protected PageDataView<TEntity> Page(string database, string tableName, Dictionary<string, BsonValue> dictionary, int pageSize, int currentPage){var where = Builders<TEntity>.Filter.Empty;if (dictionary.Count > 0){var filterBuilder = Builders<TEntity>.Filter;List<FilterDefinition<TEntity>> listFilter = new List<FilterDefinition<TEntity>>();foreach (var pair in dictionary){listFilter.Add(filterBuilder.Eq(pair.Key, pair.Value));}where = filterBuilder.And(listFilter);}PageDataView<TEntity> result = new PageDataView<TEntity>();var query = CreateQuery(database, tableName);result.TotalRecords = (int)query.CountDocuments(where);result.TotalPages = result.TotalRecords / pageSize;if (result.TotalRecords % pageSize > 0)result.TotalPages += 1;var list = query.Find(where).Skip((currentPage - 1) * pageSize).Limit(pageSize).ToList();result.Items = list;return result;}}
?
比如有個類CreatedTableLog
那個Helper就是
public class CreatedTableLogHelper: MongoBaseDAL<CreatedTableLog>{public static string Database = "Base";public static string Table = "CreatedTableLog";public CreatedTableLogHelper(IOptions<MongoConnect> options) : base(options){}public PageDataView<CreatedTableLog> GetListByPage(Dictionary<string, BsonValue> dictionary, int pageSize, int currentPage){return Page(Database, Table, dictionary, pageSize, currentPage);}}
在StartUp里面增加代碼
#region MongoDBservices.Configure<MongoConnect>(Configuration.GetSection("MongoConnect"));services.AddScoped(typeof(MongoBaseDAL<>));services.AddScoped<CreatedTableLogHelper>();#endregion
打開配置文件
appsettings.Development.json這個是DEBUG版本的配置文件
寫入配置
"MongoConnect": {"ConnectString": "192.168.50.110:27017"}
192.168.50.110是我測試環境是MongoDB服務器地址,端口默認
appsettings.Development.json這個是Release版本的配置文件可能這個地址就是localhost了,要對應更改
比如CreatedTableLog表有三個字段
UserId和NickName需要查詢
Dictionary<string, BsonValue> dictionary = new Dictionary<string, BsonValue>();var index = model.Start == 0 ? 1 : (model.Start / model.Length) + 1;if (model.UserId != 0){dictionary.Add("UserId", BsonInt64.Create(model.UserId));}if (!string.IsNullOrWhiteSpace(model.NickName)){dictionary.Add("NickName", BsonString.Create(model.NickName));}var result = CreatedTableLogHelper.GetListByPage(dictionary, model.Length, index);
這樣你以為就ok了?no no no
會報錯的,為什么同一個實體model,寫入正常,讀會報錯_id錯誤呢?
因為實體model如果沒有Id類型是ObjectId,會自動構建,但是你反序列化就會錯誤了
增加一個繼承類
public class MongoDbBase{private ObjectId _id;public ObjectId Id{get { return _id; }set { _id = value; }}}
你需要反序列化的實體對象繼承
比如CreatedTableLog改為
public class CreatedTableLog: MongoDbBase
再讀一下,對了吧?大功告成