/// <summary> /// 异步批量插入 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="t">实体集合</param> /// <returns></returns> public static async Task <int> InsertManyAsync(MongodbHostOptions host, List <T> t) { try { var client = MongodbClient <T> .MongodbInfoClient(host); await client.InsertManyAsync(t); return(1); } catch { return(0); } }
/// <summary> /// 批量插入 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="t">实体集合</param> /// <returns></returns> public static int InsertMany(MongodbHostOptions host, List <T> t) { try { var client = MongodbClient <T> .MongodbInfoClient(host); client.InsertMany(t); return(1); } catch (Exception ex) { return(0); throw ex; } }
/// <summary> /// 异步根据条件获取总数 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">条件</param> /// <returns></returns> public static async Task <long> CountAsync(MongodbHostOptions host, FilterDefinition <T> filter) { try { var client = MongodbClient <T> .MongodbInfoClient(host); #pragma warning disable CS0618 // 类型或成员已过时 return(await client.CountAsync(filter)); #pragma warning restore CS0618 // 类型或成员已过时 } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步删除一条数据 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="id">objectId</param> /// <returns></returns> public static async Task <DeleteResult> DeleteAsync(MongodbHostOptions host, string id) { try { var client = MongodbClient <T> .MongodbInfoClient(host); //修改条件 FilterDefinition <T> filter = Builders <T> .Filter.Eq("_id", new ObjectId(id)); return(await client.DeleteOneAsync(filter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 分页查询集合 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">查询条件</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">页容量</param> /// <param name="count">总条数</param> /// <param name="field">要查询的字段,不写时查询全部</param> /// <param name="sort">要排序的字段</param> /// <returns></returns> public static List <T> FindListByPage(MongodbHostOptions host, FilterDefinition <T> filter, int pageIndex, int pageSize, out long count, string[] field = null, SortDefinition <T> sort = null) { try { var client = MongodbClient <T> .MongodbInfoClient(host); #pragma warning disable CS0618 // 类型或成员已过时 count = client.Count(filter); #pragma warning restore CS0618 // 类型或成员已过时 //不指定查询字段 if (field == null || field.Length == 0) { if (sort == null) { return(client.Find(filter).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList()); } //进行排序 return(client.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList()); } //制定查询字段 var fieldList = new List <ProjectionDefinition <T> >(); for (int i = 0; i < field.Length; i++) { fieldList.Add(Builders <T> .Projection.Include(field[i].ToString())); } var projection = Builders <T> .Projection.Combine(fieldList); fieldList?.Clear(); //不排序 if (sort == null) { return(client.Find(filter).Project <T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList()); } //排序查询 return(client.Find(filter).Sort(sort).Project <T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList()); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步分页查询集合 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">查询条件</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">页容量</param> /// <param name="field">要查询的字段,不写时查询全部</param> /// <param name="sort">要排序的字段</param> /// <returns></returns> public static async Task <List <T> > FindListByPageAsync(MongodbHostOptions host, FilterDefinition <T> filter, int pageIndex, int pageSize, string[] field = null, SortDefinition <T> sort = null) { try { var client = MongodbClient <T> .MongodbInfoClient(host); //不指定查询字段 if (field == null || field.Length == 0) { if (sort == null) { return(await client.Find(filter).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync()); } //进行排序 return(await client.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync()); } //制定查询字段 var fieldList = new List <ProjectionDefinition <T> >(); for (int i = 0; i < field.Length; i++) { fieldList.Add(Builders <T> .Projection.Include(field[i].ToString())); } var projection = Builders <T> .Projection.Combine(fieldList); fieldList?.Clear(); //不排序 if (sort == null) { return(await client.Find(filter).Project <T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync()); } //排序查询 return(await client.Find(filter).Sort(sort).Project <T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync()); } catch (Exception ex) { throw ex; } }
/// <summary> /// 查询集合 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">查询条件</param> /// <param name="field">要查询的字段,不写时查询全部</param> /// <param name="sort">要排序的字段</param> /// <returns></returns> public static List <T> FindList(MongodbHostOptions host, FilterDefinition <T> filter, string[] field = null, SortDefinition <T> sort = null) { try { var client = MongodbClient <T> .MongodbInfoClient(host); //不指定查询字段 if (field == null || field.Length == 0) { if (sort == null) { return(client.Find(filter).ToList()); } //进行排序 return(client.Find(filter).Sort(sort).ToList()); } //制定查询字段 var fieldList = new List <ProjectionDefinition <T> >(); for (int i = 0; i < field.Length; i++) { fieldList.Add(Builders <T> .Projection.Include(field[i].ToString())); } var projection = Builders <T> .Projection.Combine(fieldList); fieldList?.Clear(); if (sort == null) { return(client.Find(filter).Project <T>(projection).ToList()); } //排序查询 return(client.Find(filter).Sort(sort).Project <T>(projection).ToList()); } catch (Exception ex) { throw ex; } }
public WeatherForecastController(IOptionsSnapshot <MongodbHostOptions> options, ILogger <WeatherForecastController> logger) { _logger = logger; _options = options.Value; }
/// <summary> /// 构造函数注入 /// </summary> /// <param name="mongodbHostOptions">MongodbHost信息</param> /// <param name="pictureOptions">图片选项</param> public PictureController(IOptions <MongodbHostOptions> mongodbHostOptions, IOptions <PictureOptions> pictureOptions) { _mongodbHostOptions = mongodbHostOptions.Value; _pictureOptions = pictureOptions.Value; }