/// <summary> /// 异步批量修改数据 /// </summary> /// <param name="dic">要修改的字段</param> /// <param name="host">mongodb连接信息</param> /// <param name="filter">修改条件</param> /// <returns></returns> public async Task <UpdateResult> UpdateManayAsync(string Collection, Dictionary <string, string> dic, FilterDefinition <T> filter) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); T t = new T(); //要修改的字段 var list = new List <UpdateDefinition <T> >(); foreach (var item in t.GetType().GetProperties()) { if (!dic.ContainsKey(item.Name)) { continue; } var value = dic[item.Name]; list.Add(Builders <T> .Update.Set(item.Name, value)); } var updatefilter = Builders <T> .Update.Combine(list); return(await client.UpdateManyAsync(filter, updatefilter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步根据id查询一条数据 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="id">objectid</param> /// <returns></returns> public async Task <T> GetAsync(string Collection, string id, string[] field = null) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); FilterDefinition <T> filter = Builders <T> .Filter.Eq("_id", new ObjectId(id)); //不指定查询字段 if (field == null || field.Length == 0) { return(await client.Find(filter).FirstOrDefaultAsync()); } //指定查询字段 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(); return(await client.Find(filter).Project <T>(projection).FirstOrDefaultAsync()); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步修改一条数据 /// </summary> /// <param name="t">添加的实体</param> /// <param name="host">mongodb连接信息</param> /// <returns></returns> public async Task <UpdateResult> UpdateAsync(string Collection, T t, string id) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); //修改条件 FilterDefinition <T> filter = Builders <T> .Filter.Eq("_id", new ObjectId(id)); //要修改的字段 var list = new List <UpdateDefinition <T> >(); foreach (var item in t.GetType().GetProperties()) { if (item.Name.ToLower() == "id") { continue; } list.Add(Builders <T> .Update.Set(item.Name, item.GetValue(t))); } var updatefilter = Builders <T> .Update.Combine(list); return(await client.UpdateOneAsync(filter, updatefilter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 根据条件获取总数 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">条件</param> /// <returns></returns> public long Count(string Collection, FilterDefinition <T> filter) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); return(client.Count(filter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步删除多条数据 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">删除的条件</param> /// <returns></returns> public async Task <DeleteResult> DeleteManyAsync(string Collection, FilterDefinition <T> filter) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); return(await client.DeleteManyAsync(filter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 删除多条数据 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="filter">删除的条件</param> /// <returns></returns> public DeleteResult DeleteMany(string Collection, FilterDefinition <T> filter) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); return(client.DeleteMany(filter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 添加一条数据 /// </summary> /// <param name="t">添加的实体</param> /// <param name="host">mongodb连接信息</param> /// <returns></returns> public T Add2(string Collection, T t) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); client.InsertOne(t); return(t); } catch (Exception ex) { throw ex; } }
/// <summary> /// 批量插入 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="t">实体集合</param> /// <returns></returns> public bool AddList(string Collection, List <T> t) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); client.InsertMany(t); return(true); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步添加一条数据 /// </summary> /// <param name="t">添加的实体</param> /// <param name="host">mongodb连接信息</param> /// <returns></returns> public async Task <bool> AddAsync(string Collection, T t) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); await client.InsertOneAsync(t); return(true); } catch (Exception ex) { throw ex; } }
/// <summary> /// 删除一条数据 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="id">objectId</param> /// <returns></returns> public DeleteResult Delete(string Collection, string id) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); FilterDefinition <T> filter = Builders <T> .Filter.Eq("_id", new ObjectId(id)); return(client.DeleteOne(filter)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 异步删除一条数据 /// </summary> /// <param name="host">mongodb连接信息</param> /// <param name="id">objectId</param> /// <returns></returns> public async Task <DeleteResult> DeleteAsync(string Collection, string id) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); //修改条件 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">页码,从1开始</param> /// <param name="pageSize">页数据量</param> /// <param name="count">总条数</param> /// <param name="field">要查询的字段,不写时查询全部</param> /// <param name="sort">要排序的字段</param> /// <returns></returns> public List <T> GetList(string Collection, FilterDefinition <T> filter, int pageIndex, int pageSize, out long count, string[] field = null, SortDefinition <T> sort = null) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); count = Convert.ToInt32(client.CountDocuments(filter)); if (pageIndex < 1 || pageSize < 1) { return(null); } //不指定查询字段 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="field">要查询的字段,不写时查询全部</param> /// <param name="sort">要排序的字段</param> /// <returns></returns> public List <T> GetList(string Collection, FilterDefinition <T> filter, string[] field = null, SortDefinition <T> sort = null) { try { var client = MongoDBClient <T> .GetCollectionInstance(Collection); //不指定查询字段 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; } }