liulufei 2020-05-09
1. 依赖包 MongoDB.Driver; MongoDB.Json; MongoDB.Bson;
2. 上代码
using MongoDB.Driver; using System; using System.Collections.Generic; using System.Text; using System.Linq; using System.Linq.Expressions; using System.Reflection; using MongoDB.Driver.Linq; namespace Common { public class MongodbHelper { protected static MongoClient client; public MongodbHelper() { client = new MongoClient("mongodb://localhost:27017"); } static MongodbHelper() { client = new MongoClient("mongodb://localhost:27017"); } /// <summary> /// 获取 Collection 信息 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="collName">Collection 名称</param> /// <param name="dbName">DBase名称</param> /// <returns></returns> public static MyCollection<T> GetCollection<T>(string collName, string dbName) { MyCollection<T> mycollection = new MyCollection<T>(); IMongoDatabase database = client.GetDatabase(dbName); IMongoCollection<T> collection = database.GetCollection<T>(collName); mycollection.collection = collection; return mycollection; } } public class MyCollection<T> { public IMongoCollection<T> collection; /// <summary> /// 查询数据 延迟加载 后期可以使用Linq Lambda处理数据 /// </summary> /// <returns></returns> public IMongoQueryable<T> QueryData() { var list = collection.AsQueryable<T>(); return list; } /// <summary> /// 查询所有数据 /// </summary> /// <param name="expression"></param> /// <returns></returns> public List<T> QueryData(Expression<Func<T, bool>> expression) { var list = collection.AsQueryable().Where(expression); return list.ToList<T>(); } /// <summary> /// 分页查询 /// </summary> /// <param name="expressio"></param> /// <param name="pageInfo"></param> /// <returns></returns> public PageInfo<T> QueryData(Expression<Func<T, bool>> expressio, PageInfo<T> pageInfo) { List<T> list = null; pageInfo.Totoal = collection.AsQueryable<T>().Count(); pageInfo.PageNum = (int)Math.Ceiling(pageInfo.Totoal / pageInfo.PageSize * 0.1); if (pageInfo == null || pageInfo.IsAll == true) if (expressio != null) list = collection.AsQueryable<T>().Where(expressio).ToList(); else list = collection.AsQueryable<T>().ToList(); else if (expressio != null) { list = collection.AsQueryable<T>().Where(expressio).Skip(pageInfo.PageSize * (pageInfo.PageIndex - 1)).Take(pageInfo.PageSize).ToList(); } else { list = collection.AsQueryable<T>().Skip(pageInfo.PageSize * (pageInfo.PageIndex - 1)).Take(pageInfo.PageSize).ToList(); } pageInfo.Data = list; return pageInfo; } /// <summary> /// 新增一条数据(文档) /// </summary> /// <param name="ts"></param> public void AddDoc(T ts) { collection.InsertOne(ts); } /// <summary> /// 批量新增多个文档 /// </summary> /// <param name="ts"></param> public void AddDocs(List<T> ts) { collection.InsertMany(ts); } /// <summary> /// 更新文档 不存在就新增 /// </summary> /// <param name="filter"></param> /// <param name="t"></param> public void UpdateDoc(Expression<Func<T, bool>> filter, T t) { // FilterDefinition<T> filter = null; // UpdateDefinition<T> update = Builders<T>.Update.ToBsonDocument();// null;// Builders<T>.Update. var newData = BuildQueryOption(t); UpdateResult result = collection.UpdateOne(filter, newData, new UpdateOptions { IsUpsert = true }); } /// <summary> /// 删除文档 /// </summary> /// <param name="predicate"></param> public void Detele(Expression<Func<T, bool>> predicate) { var result = collection.DeleteMany(predicate);//.ConfigureAwait(false); // return result.DeletedCount; } /// <summary> /// 利用反射创建 更新字段 (这里没有处理空) /// </summary> /// <param name="doc"></param> /// <returns></returns> private UpdateDefinition<T> BuildQueryOption(T doc) { var update = Builders<T>.Update; var updates = new List<UpdateDefinition<T>>(); var t = doc.GetType(); var proper = t.GetProperties(); foreach (PropertyInfo info in proper) { var value = info.GetValue(doc); if (value != null) { updates.Add(update.Set(info.Name, info.GetValue(doc))); } } // update.Combine(updates); return update.Combine(updates); } } /// <summary> /// 分页信息 /// </summary> /// <typeparam name="T"></typeparam> public class PageInfo<T> { public bool IsAll { get; set; } = false; public int PageSize { get; set; } = 100; public int PageIndex { get; set; } = 1; public long Totoal { get; set; } public int PageNum { get; set; } public List<T> Data { get; set; } } }
3. 测试方法
using Common; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; namespace ZhiHuHot { public class TestMongo { public void GetHot() { MyCollection<HotInfo> collection = MongodbHelper.GetCollection<HotInfo>("ZhiHuHot", "ZhiHu"); Expression<Func<HotInfo, bool>> predicate = null; predicate = a => a.HotID.Equals(391481443); PageInfo<HotInfo> hots = collection.QueryData(null, new PageInfo<HotInfo>()); } } }