ExternalAPI_NET8/Common/MongoDB/MongoDBHelper.cs
2026-05-21 17:23:36 +08:00

141 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<T>:IMongoDBHelper<T> 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<T> GetColletion(string collName)
{
return database.GetCollection<T>(collName);
}
#region
/// <summary>
/// 查询复杂查询直接用Linq处理
/// </summary>
/// <param name="collName">集合名称</param>
/// <returns>要查询的对象</returns>
private IQueryable<T> GetQueryable(string collName)
{
var coll = GetColletion(collName);
return coll.AsQueryable<T>();
}
/// <summary>
/// 根据条件表达式返回可查询的记录源
/// </summary>
/// <param name="query">查询条件</param>
/// <param name="sortPropertyName">排序表达式</param>
/// <param name="isDescending">如果为true则为降序否则为升序</param>
/// <returns></returns>
private IFindFluent<T, T> GetQueryable(string collName, FilterDefinition<T> query, string sortPropertyName, bool isDescending = true)
{
IMongoCollection<T> collection = GetColletion(collName);
IFindFluent<T, T> queryable = collection.Find(query);
var sort = isDescending ? Builders<T>.Sort.Descending(sortPropertyName) : Builders<T>.Sort.Ascending(sortPropertyName);
return queryable.Sort(sort);
}
/// <summary>
/// 根据条件表达式返回可查询的记录源
/// </summary>
/// <param name="match">查询条件</param>
/// <param name="orderByProperty">排序表达式</param>
/// <param name="isDescending">如果为true则为降序否则为升序</param>
/// <returns></returns>
private IQueryable<T> GetQueryable<TKey>(string collName, Expression<Func<T, bool>> match, Expression<Func<T, TKey>> orderByProperty, bool isDescending = true)
{
IMongoCollection<T> collection = GetColletion(collName);
IQueryable<T> 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;
}
/// <summary>
/// 根据条件查询数据库,并返回对象集合
/// </summary>
/// <param name="match">条件表达式</param>
/// <param name="sortPropertyName">排序字段</param>
/// <param name="isDescending">如果为true则为降序否则为升序</param>
/// <returns></returns>
public IList<T> Find(string collName, Expression<Func<T, bool>> match, string sortPropertyName, bool isDescending = true)
{
return GetQueryable(collName, match, sortPropertyName, isDescending).ToList();
}
/// <summary>
/// 根据条件查询数据库,并返回对象集合
/// </summary>
/// <param name="query">条件表达式</param>
/// <param name="sortPropertyName">排序字段</param>
/// <param name="isDescending">如果为true则为降序否则为升序</param>
/// <returns></returns>
public IList<T> Find(string collName, FilterDefinition<T> query, string sortPropertyName, bool isDescending = true)
{
return GetQueryable(collName, query, sortPropertyName, isDescending).ToList();
}
/// <summary>
/// 根据条件表达式返回可查询的记录源(异步方法)
/// </summary>
/// <param name="collName"></param>
/// <param name="query"></param>
/// <returns></returns>
public async Task<IList<T>> FindAsync(string collName, FilterDefinition<T> query)
{
IMongoCollection<T> collection = GetColletion(collName);
var cursor = await collection.FindAsync(query);
return await cursor.ToListAsync();
}
/// <summary>
/// 根据条件查询数据库,并返回对象集合
/// </summary>
/// <param name="match">条件表达式</param>
/// <param name="orderByProperty">排序表达式</param>
/// <param name="isDescending">如果为true则为降序否则为升序</param>
/// <returns></returns>
public IList<T> Find<TKey>(string collName, Expression<Func<T, bool>> match, Expression<Func<T, TKey>> orderByProperty, bool isDescending = true)
{
return GetQueryable<TKey>(collName, match, orderByProperty, isDescending).ToList();
}
/// <summary>
/// 根据条件查询数据库,如果存在返回第一个对象
/// </summary>
/// <param name="filter">条件表达式</param>
/// <returns>存在则返回指定的第一个对象,否则返回默认值</returns>
public T FindSingle(string collName, FilterDefinition<T> filter)
{
var coll = GetColletion(collName);
return coll.Find(filter).FirstOrDefault();
}
#endregion
}