using System.Linq.Expressions; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; using Web.Core.Common.Helper; using Web.Core.Model.Entity; namespace Web.Core.Common.MongoDB; public class MongoDBHelper:IMongoDBHelper where T : MDBBaseEntity { private readonly IMongoDatabase database; public MongoDBHelper() { var url = new MongoUrl(AppSettings.GetConnectionString("MongoDB")); MongoClientSettings mcs = MongoClientSettings.FromUrl(url); mcs.MaxConnectionLifeTime = TimeSpan.FromMilliseconds(1000); var client = new MongoClient(mcs); database = client.GetDatabase(url.DatabaseName); } private IMongoCollection GetColletion(string collName) { return database.GetCollection(collName); } #region 查询 /// /// 查询,复杂查询直接用Linq处理 /// /// 集合名称 /// 要查询的对象 private IQueryable GetQueryable(string collName) { var coll = GetColletion(collName); return coll.AsQueryable(); } /// /// 根据条件表达式返回可查询的记录源 /// /// 查询条件 /// 排序表达式 /// 如果为true则为降序,否则为升序 /// private IFindFluent GetQueryable(string collName, FilterDefinition query, string sortPropertyName, bool isDescending = true) { IMongoCollection collection = GetColletion(collName); IFindFluent queryable = collection.Find(query); var sort = isDescending ? Builders.Sort.Descending(sortPropertyName) : Builders.Sort.Ascending(sortPropertyName); return queryable.Sort(sort); } /// /// 根据条件表达式返回可查询的记录源 /// /// 查询条件 /// 排序表达式 /// 如果为true则为降序,否则为升序 /// private IQueryable GetQueryable(string collName, Expression> match, Expression> orderByProperty, bool isDescending = true) { IMongoCollection collection = GetColletion(collName); IQueryable query = collection.AsQueryable(); if (match != null) { query = query.Where(match); } if (orderByProperty != null) { query = isDescending ? query.OrderByDescending(orderByProperty) : query.OrderBy(orderByProperty); } else { // query = query.OrderBy(sortPropertyName, isDescending); } return query; } /// /// 根据条件查询数据库,并返回对象集合 /// /// 条件表达式 /// 排序字段 /// 如果为true则为降序,否则为升序 /// public IList Find(string collName, Expression> match, string sortPropertyName, bool isDescending = true) { return GetQueryable(collName, match, sortPropertyName, isDescending).ToList(); } /// /// 根据条件查询数据库,并返回对象集合 /// /// 条件表达式 /// 排序字段 /// 如果为true则为降序,否则为升序 /// public IList Find(string collName, FilterDefinition query, string sortPropertyName, bool isDescending = true) { return GetQueryable(collName, query, sortPropertyName, isDescending).ToList(); } /// /// 根据条件表达式返回可查询的记录源(异步方法) /// /// /// /// public async Task> FindAsync(string collName, FilterDefinition query) { IMongoCollection collection = GetColletion(collName); var cursor = await collection.FindAsync(query); return await cursor.ToListAsync(); } /// /// 根据条件查询数据库,并返回对象集合 /// /// 条件表达式 /// 排序表达式 /// 如果为true则为降序,否则为升序 /// public IList Find(string collName, Expression> match, Expression> orderByProperty, bool isDescending = true) { return GetQueryable(collName, match, orderByProperty, isDescending).ToList(); } /// /// 根据条件查询数据库,如果存在返回第一个对象 /// /// 条件表达式 /// 存在则返回指定的第一个对象,否则返回默认值 public T FindSingle(string collName, FilterDefinition filter) { var coll = GetColletion(collName); return coll.Find(filter).FirstOrDefault(); } #endregion }