.net core MongoDB 初试
是这样的,我们有一个场景,另一个服务器是写到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 MongoDB services.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
再读一下,对了吧?大功告成