日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

.net core MongoDB 初试

發布時間:2023/12/9 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .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 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

再讀一下,對了吧?大功告成

轉載于:https://www.cnblogs.com/NCoreCoder/p/9870030.html

總結

以上是生活随笔為你收集整理的.net core MongoDB 初试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。