MongoDB的基本操作类库
封装了mongodb的基本操作类库,总结查询

1 public static class MongodbHelper<T> where T : class, new() 2 { 3 #region +Add 添加一条数据 4 /// <summary> 5 /// 添加一条数据 6 /// </summary> 7 /// <param name="t">添加的实体</param> 8 /// <param name="host">mongodb连接信息</param> 9 /// <returns></returns> 10 public static int Add(MongodbHostConfig host, T t) 11 { 12 try 13 { 14 var client = MongodbClient<T>.MongodbInfoClient(host); 15 client.InsertOne(t); 16 return 1; 17 } 18 catch 19 { 20 return 0; 21 } 22 } 23 #endregion 24 25 #region +AddAsync 异步添加一条数据 26 /// <summary> 27 /// 异步添加一条数据 28 /// </summary> 29 /// <param name="t">添加的实体</param> 30 /// <param name="host">mongodb连接信息</param> 31 /// <returns></returns> 32 public static async Task<int> AddAsync(MongodbHostConfig host, T t) 33 { 34 try 35 { 36 var client = MongodbClient<T>.MongodbInfoClient(host); 37 await client.InsertOneAsync(t); 38 return 1; 39 } 40 catch 41 { 42 return 0; 43 } 44 } 45 46 public static Task<int> GridFSAddAsync(MongodbHostConfig host, T t) 47 { 48 try 49 { 50 var client = MongodbClient<T>.MongodbInfoClient(host); 51 52 return Task.FromResult(1); 53 } 54 catch 55 { 56 return Task.FromResult(0); 57 } 58 } 59 #endregion 60 61 #region +InsertMany 批量插入 62 /// <summary> 63 /// 批量插入 64 /// </summary> 65 /// <param name="host">mongodb连接信息</param> 66 /// <param name="t">实体集合</param> 67 /// <returns></returns> 68 public static int InsertMany(MongodbHostConfig host, List<T> t) 69 { 70 try 71 { 72 var client = MongodbClient<T>.MongodbInfoClient(host); 73 client.InsertMany(t); 74 return 1; 75 } 76 catch (Exception ex) 77 { 78 return 0; 79 throw ex; 80 } 81 } 82 #endregion 83 84 #region +InsertManyAsync 异步批量插入 85 /// <summary> 86 /// 异步批量插入 87 /// </summary> 88 /// <param name="host">mongodb连接信息</param> 89 /// <param name="t">实体集合</param> 90 /// <returns></returns> 91 public static async Task<int> InsertManyAsync(MongodbHostConfig host, List<T> t) 92 { 93 try 94 { 95 var client = MongodbClient<T>.MongodbInfoClient(host); 96 await client.InsertManyAsync(t); 97 return 1; 98 } 99 catch 100 { 101 return 0; 102 } 103 } 104 #endregion 105 106 #region +Update 修改一条数据 107 /// <summary> 108 /// 修改一条数据 109 /// </summary> 110 /// <param name="t">添加的实体</param> 111 /// <param name="host">mongodb连接信息</param> 112 /// <param name="id">主键</param> 113 /// <returns></returns> 114 public static UpdateResult Update(MongodbHostConfig host, T t, string id) 115 { 116 try 117 { 118 var client = MongodbClient<T>.MongodbInfoClient(host); 119 //修改条件 120 FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(id)); 121 //要修改的字段 122 var list = new List<UpdateDefinition<T>>(); 123 foreach (var item in t.GetType().GetProperties()) 124 { 125 if (item.Name.ToLower() == "id") continue; 126 list.Add(Builders<T>.Update.Set(item.Name, item.GetValue(t))); 127 } 128 var updatefilter = Builders<T>.Update.Combine(list); 129 return client.UpdateOne(filter, updatefilter); 130 } 131 catch (Exception ex) 132 { 133 throw ex; 134 } 135 } 136 #endregion 137 138 #region +UpdateAsync 异步修改一条数据 139 /// <summary> 140 /// 异步修改一条数据 141 /// </summary> 142 /// <param name="t">添加的实体</param> 143 /// <param name="host">mongodb连接信息</param> 144 /// <param name="id">主键</param> 145 /// <returns></returns> 146 public static async Task<UpdateResult> UpdateAsync(MongodbHostConfig host, T t, string id) 147 { 148 try 149 { 150 var client = MongodbClient<T>.MongodbInfoClient(host); 151 //修改条件 152 FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(id)); 153 //要修改的字段 154 var list = new List<UpdateDefinition<T>>(); 155 foreach (var item in t.GetType().GetProperties()) 156 { 157 if (item.Name.ToLower() == "id") continue; 158 list.Add(Builders<T>.Update.Set(item.Name, item.GetValue(t))); 159 } 160 var updatefilter = Builders<T>.Update.Combine(list); 161 return await client.UpdateOneAsync(filter, updatefilter); 162 } 163 catch (Exception ex) 164 { 165 throw ex; 166 } 167 } 168 #endregion 169 170 #region +UpdateManay 批量修改数据 171 /// <summary> 172 /// 批量修改数据 173 /// </summary> 174 /// <param name="dic">要修改的字段</param> 175 /// <param name="host">mongodb连接信息</param> 176 /// <param name="filter">修改条件</param> 177 /// <returns></returns> 178 public static UpdateResult UpdateManay(MongodbHostConfig host, Dictionary<string, string> dic, FilterDefinition<T> filter) 179 { 180 try 181 { 182 var client = MongodbClient<T>.MongodbInfoClient(host); 183 T t = new T(); 184 //要修改的字段 185 var list = new List<UpdateDefinition<T>>(); 186 foreach (var item in t.GetType().GetProperties()) 187 { 188 if (!dic.ContainsKey(item.Name)) continue; 189 var value = dic[item.Name]; 190 list.Add(Builders<T>.Update.Set(item.Name, value)); 191 } 192 var updatefilter = Builders<T>.Update.Combine(list); 193 return client.UpdateMany(filter, updatefilter); 194 } 195 catch (Exception ex) 196 { 197 throw ex; 198 } 199 } 200 #endregion 201 202 #region +UpdateManayAsync 异步批量修改数据 203 /// <summary> 204 /// 异步批量修改数据 205 /// </summary> 206 /// <param name="dic">要修改的字段</param> 207 /// <param name="host">mongodb连接信息</param> 208 /// <param name="filter">修改条件</param> 209 /// <returns></returns> 210 public static async Task<UpdateResult> UpdateManayAsync(MongodbHostConfig host, Dictionary<string, string> dic, FilterDefinition<T> filter) 211 { 212 try 213 { 214 var client = MongodbClient<T>.MongodbInfoClient(host); 215 T t = new T(); 216 //要修改的字段 217 var list = new List<UpdateDefinition<T>>(); 218 foreach (var item in t.GetType().GetProperties()) 219 { 220 if (!dic.ContainsKey(item.Name)) continue; 221 var value = dic[item.Name]; 222 list.Add(Builders<T>.Update.Set(item.Name, value)); 223 } 224 var updatefilter = Builders<T>.Update.Combine(list); 225 return await client.UpdateManyAsync(filter, updatefilter); 226 } 227 catch (Exception ex) 228 { 229 throw ex; 230 } 231 } 232 #endregion 233 234 #region Delete 删除一条数据 235 /// <summary> 236 /// 删除一条数据 237 /// </summary> 238 /// <param name="host">mongodb连接信息</param> 239 /// <param name="id">objectId</param> 240 /// <returns></returns> 241 public static DeleteResult Delete(MongodbHostConfig host, string id) 242 { 243 try 244 { 245 var client = MongodbClient<T>.MongodbInfoClient(host); 246 FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(id)); 247 return client.DeleteOne(filter); 248 } 249 catch (Exception ex) 250 { 251 throw ex; 252 } 253 254 } 255 #endregion 256 257 #region DeleteAsync 异步删除一条数据 258 /// <summary> 259 /// 异步删除一条数据 260 /// </summary> 261 /// <param name="host">mongodb连接信息</param> 262 /// <param name="id">objectId</param> 263 /// <returns></returns> 264 public static async Task<DeleteResult> DeleteAsync(MongodbHostConfig host, string id) 265 { 266 try 267 { 268 var client = MongodbClient<T>.MongodbInfoClient(host); 269 //修改条件 270 FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(id)); 271 return await client.DeleteOneAsync(filter); 272 } 273 catch (Exception ex) 274 { 275 throw ex; 276 } 277 278 } 279 #endregion 280 281 #region DeleteMany 删除多条数据 282 /// <summary> 283 /// 删除一条数据 284 /// </summary> 285 /// <param name="host">mongodb连接信息</param> 286 /// <param name="filter">删除的条件</param> 287 /// <returns></returns> 288 public static DeleteResult DeleteMany(MongodbHostConfig host, FilterDefinition<T> filter) 289 { 290 try 291 { 292 var client = MongodbClient<T>.MongodbInfoClient(host); 293 return client.DeleteMany(filter); 294 } 295 catch (Exception ex) 296 { 297 throw ex; 298 } 299 300 } 301 #endregion 302 303 #region DeleteManyAsync 异步删除多条数据 304 /// <summary> 305 /// 异步删除多条数据 306 /// </summary> 307 /// <param name="host">mongodb连接信息</param> 308 /// <param name="filter">删除的条件</param> 309 /// <returns></returns> 310 public static async Task<DeleteResult> DeleteManyAsync(MongodbHostConfig host, FilterDefinition<T> filter) 311 { 312 try 313 { 314 var client = MongodbClient<T>.MongodbInfoClient(host); 315 return await client.DeleteManyAsync(filter); 316 } 317 catch (Exception ex) 318 { 319 throw ex; 320 } 321 322 } 323 #endregion 324 325 #region Count 根据条件获取总数 326 /// <summary> 327 /// 根据条件获取总数 328 /// </summary> 329 /// <param name="host">mongodb连接信息</param> 330 /// <param name="filter">条件</param> 331 /// <returns></returns> 332 public static long Count(MongodbHostConfig host, FilterDefinition<T> filter) 333 { 334 try 335 { 336 var client = MongodbClient<T>.MongodbInfoClient(host); 337 #pragma warning disable CS0618 // 类型或成员已过时 338 return client.Count(filter); 339 #pragma warning restore CS0618 // 类型或成员已过时 340 } 341 catch (Exception ex) 342 { 343 throw ex; 344 } 345 } 346 #endregion 347 348 #region CountAsync 异步根据条件获取总数 349 /// <summary> 350 /// 异步根据条件获取总数 351 /// </summary> 352 /// <param name="host">mongodb连接信息</param> 353 /// <param name="filter">条件</param> 354 /// <returns></returns> 355 public static async Task<long> CountAsync(MongodbHostConfig host, FilterDefinition<T> filter) 356 { 357 try 358 { 359 var client = MongodbClient<T>.MongodbInfoClient(host); 360 #pragma warning disable CS0618 // 类型或成员已过时 361 return await client.CountAsync(filter); 362 #pragma warning restore CS0618 // 类型或成员已过时 363 } 364 catch (Exception ex) 365 { 366 throw ex; 367 } 368 } 369 #endregion 370 371 #region FindOne 根据id查询一条数据 372 /// <summary> 373 /// 根据id查询一条数据 374 /// </summary> 375 /// <param name="host">mongodb连接信息</param> 376 /// <param name="id">objectid</param> 377 /// <param name="field">要查询的字段,不写时查询全部</param> 378 /// <returns></returns> 379 public static T FindOne(MongodbHostConfig host, string id, string[] field = null) 380 { 381 try 382 { 383 var client = MongodbClient<T>.MongodbInfoClient(host); 384 FilterDefinition<T> filter = Builders<T>.Filter.Eq("_id", new ObjectId(id)); 385 //不指定查询字段 386 if (field == null || field.Length == 0) 387 { 388 return client.Find(filter).FirstOrDefault<T>(); 389 } 390 391 //制定查询字段 392 var fieldList = new List<ProjectionDefinition<T>>(); 393 for (int i = 0; i < field.Length; i++) 394 { 395 fieldList.Add(Builders<T>.Projection.Include(field[i].ToString())); 396 } 397 var projection = Builders<T>.Projection.Combine(fieldList); 398 fieldList?.Clear(); 399 return client.Find(filter).Project<T>(projection).FirstOrDefault<T>(); 400 } 401 catch (Exception ex) 402 { 403 throw ex; 404 } 405 } 406 #endregion 407 408 #region FindOneAsync 异步根据id查询一条数据 409 /// <summary> 410 /// 异步根据id查询一条数据 411 /// </summary> 412 /// <param name="host">mongodb连接信息</param> 413 /// <param name="id">objectId</param> 414 /// <param name="field">字段名称</param> 415 /// <returns></returns> 416 public static async Task<T> FindOneAsync(MongodbHostConfig host, string id, string[] field = null) 417 { 418 var client = MongodbClient<T>.MongodbInfoClient(host); 419 var filter = Builders<T>.Filter.Eq("_id", new ObjectId(id)); 420 //不指定查询字段 421 if (field == null || field.Length == 0) 422 { 423 return await client.Find(filter).FirstOrDefaultAsync(); 424 } 425 426 //制定查询字段 427 var fieldList = field.Select(t => Builders<T>.Projection.Include(t.ToString())).ToList(); 428 var projection = Builders<T>.Projection.Combine(fieldList); 429 fieldList.Clear(); 430 return await client.Find(filter).Project<T>(projection).FirstOrDefaultAsync(); 431 } 432 433 /// <summary> 434 /// 异步根据指定的列查询一条数据 435 /// </summary> 436 /// <param name="host">mongodb连接信息</param> 437 /// <param name="columnName">列名</param> 438 /// <param name="columnValue">列值</param> 439 /// <returns></returns> 440 public static async Task<T> FindOneAsync(MongodbHostConfig host, string columnName, string columnValue) 441 { 442 var client = MongodbClient<T>.MongodbInfoClient(host); 443 var filter = Builders<T>.Filter.Eq(columnName, columnValue); 444 return await client.Find(filter).FirstOrDefaultAsync(); 445 } 446 #endregion 447 448 #region FindList 查询集合 449 /// <summary> 450 /// 查询集合 451 /// </summary> 452 /// <param name="host">mongodb连接信息</param> 453 /// <param name="filter">查询条件</param> 454 /// <param name="field">要查询的字段,不写时查询全部</param> 455 /// <param name="sort">要排序的字段</param> 456 /// <returns></returns> 457 public static List<T> FindList(MongodbHostConfig host, FilterDefinition<T> filter, string[] field = null, SortDefinition<T> sort = null) 458 { 459 try 460 { 461 var client = MongodbClient<T>.MongodbInfoClient(host); 462 //不指定查询字段 463 if (field == null || field.Length == 0) 464 { 465 if (sort == null) return client.Find(filter).ToList(); 466 //进行排序 467 return client.Find(filter).Sort(sort).ToList(); 468 } 469 470 //制定查询字段 471 var fieldList = new List<ProjectionDefinition<T>>(); 472 for (int i = 0; i < field.Length; i++) 473 { 474 fieldList.Add(Builders<T>.Projection.Include(field[i].ToString())); 475 } 476 var projection = Builders<T>.Projection.Combine(fieldList); 477 fieldList?.Clear(); 478 if (sort == null) return client.Find(filter).Project<T>(projection).ToList(); 479 //排序查询 480 return client.Find(filter).Sort(sort).Project<T>(projection).ToList(); 481 } 482 catch (Exception ex) 483 { 484 throw ex; 485 } 486 } 487 #endregion 488 489 #region FindListAsync 异步查询集合 490 /// <summary> 491 /// 异步查询集合 492 /// </summary> 493 /// <param name="host">mongodb连接信息</param> 494 /// <param name="filter">查询条件</param> 495 /// <param name="field">要查询的字段,不写时查询全部</param> 496 /// <param name="sort">要排序的字段</param> 497 /// <returns></returns> 498 public static async Task<List<T>> FindListAsync(MongodbHostConfig host, FilterDefinition<T> filter, string[] field = null, SortDefinition<T> sort = null) 499 { 500 try 501 { 502 var client = MongodbClient<T>.MongodbInfoClient(host); 503 //不指定查询字段 504 if (field == null || field.Length == 0) 505 { 506 if (sort == null) return await client.Find(filter).ToListAsync(); 507 return await client.Find(filter).Sort(sort).ToListAsync(); 508 } 509 510 //制定查询字段 511 var fieldList = new List<ProjectionDefinition<T>>(); 512 for (int i = 0; i < field.Length; i++) 513 { 514 fieldList.Add(Builders<T>.Projection.Include(field[i].ToString())); 515 } 516 var projection = Builders<T>.Projection.Combine(fieldList); 517 fieldList?.Clear(); 518 if (sort == null) return await client.Find(filter).Project<T>(projection).ToListAsync(); 519 //排序查询 520 return await client.Find(filter).Sort(sort).Project<T>(projection).ToListAsync(); 521 } 522 catch (Exception ex) 523 { 524 throw ex; 525 } 526 } 527 #endregion 528 529 #region FindListByPage 分页查询集合 530 /// <summary> 531 /// 分页查询集合 532 /// </summary> 533 /// <param name="host">mongodb连接信息</param> 534 /// <param name="filter">查询条件</param> 535 /// <param name="pageIndex">当前页</param> 536 /// <param name="pageSize">页容量</param> 537 /// <param name="count">总条数</param> 538 /// <param name="field">要查询的字段,不写时查询全部</param> 539 /// <param name="sort">要排序的字段</param> 540 /// <returns></returns> 541 public static List<T> FindListByPage(MongodbHostConfig host, FilterDefinition<T> filter, int pageIndex, int pageSize, out long count, string[] field = null, SortDefinition<T> sort = null) 542 { 543 try 544 { 545 var client = MongodbClient<T>.MongodbInfoClient(host); 546 #pragma warning disable CS0618 // 类型或成员已过时 547 count = client.Count(filter); 548 #pragma warning restore CS0618 // 类型或成员已过时 549 //不指定查询字段 550 if (field == null || field.Length == 0) 551 { 552 if (sort == null) return client.Find(filter).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList(); 553 //进行排序 554 return client.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList(); 555 } 556 557 //制定查询字段 558 var fieldList = new List<ProjectionDefinition<T>>(); 559 for (int i = 0; i < field.Length; i++) 560 { 561 fieldList.Add(Builders<T>.Projection.Include(field[i].ToString())); 562 } 563 var projection = Builders<T>.Projection.Combine(fieldList); 564 fieldList?.Clear(); 565 566 //不排序 567 if (sort == null) return client.Find(filter).Project<T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList(); 568 569 //排序查询 570 return client.Find(filter).Sort(sort).Project<T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList(); 571 572 } 573 catch (Exception ex) 574 { 575 throw ex; 576 } 577 } 578 #endregion 579 580 #region FindListByPageAsync 异步分页查询集合 581 /// <summary> 582 /// 异步分页查询集合 583 /// </summary> 584 /// <param name="host">mongodb连接信息</param> 585 /// <param name="filter">查询条件</param> 586 /// <param name="pageIndex">当前页</param> 587 /// <param name="pageSize">页容量</param> 588 /// <param name="field">要查询的字段,不写时查询全部</param> 589 /// <param name="sort">要排序的字段</param> 590 /// <returns></returns> 591 public static async Task<List<T>> FindListByPageAsync(MongodbHostConfig host, FilterDefinition<T> filter, int pageIndex, int pageSize, string[] field = null, SortDefinition<T> sort = null) 592 { 593 try 594 { 595 var client = MongodbClient<T>.MongodbInfoClient(host); 596 //不指定查询字段 597 if (field == null || field.Length == 0) 598 { 599 if (sort == null) return await client.Find(filter).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync(); 600 //进行排序 601 return await client.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync(); 602 } 603 604 //制定查询字段 605 var fieldList = new List<ProjectionDefinition<T>>(); 606 for (int i = 0; i < field.Length; i++) 607 { 608 fieldList.Add(Builders<T>.Projection.Include(field[i].ToString())); 609 } 610 var projection = Builders<T>.Projection.Combine(fieldList); 611 fieldList?.Clear(); 612 613 //不排序 614 if (sort == null) return await client.Find(filter).Project<T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync(); 615 616 //排序查询 617 return await client.Find(filter).Sort(sort).Project<T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync(); 618 619 } 620 catch (Exception ex) 621 { 622 throw ex; 623 } 624 } 625 #endregion 626 627 #region GridFS 628 629 #endregion 630 631 }
配置连接对象

1 public class MongodbHostConfig 2 { 3 /// <summary> 4 /// 连接地址 5 /// </summary> 6 public string Connection { get; set; } 7 /// <summary> 8 /// 数据库 9 /// </summary> 10 public string DataBase { get; set; } 11 12 /// <summary> 13 /// 表明 14 /// </summary> 15 public string Table { get; set; } 16 }
MongodbInfoClient 获取mongodb实例

1 public static class MongodbClient<T> where T : class 2 { 3 #region +MongodbInfoClient 获取mongodb实例 4 /// <summary> 5 /// 获取mongodb实例 6 /// </summary> 7 /// <param name="host">连接字符串,库,表</param> 8 /// <returns></returns> 9 public static IMongoCollection<T> MongodbInfoClient(MongodbHostConfig host) 10 { 11 12 MongoClient client = new MongoClient(host.Connection); 13 var dataBase = client.GetDatabase(host.DataBase); 14 if (string.IsNullOrEmpty(host.Table)) 15 { 16 return dataBase.GetCollection<T>(typeof(T).Name); 17 } 18 else 19 { 20 return dataBase.GetCollection<T>(host.Table); 21 } 22 23 } 24 25 #endregion 26 }
调用mongodb操作类

1 #region Methods 2 /// <summary> 3 /// 获取收藏店铺列表 4 /// </summary> 5 /// <param name="pageSize"></param> 6 /// <param name="userId"></param> 7 /// <param name="pageIndex"></param> 8 /// <returns></returns> 9 public async Task<PagedList<StoreCollectionOutput>> GetStoreCollections(int pageIndex, int pageSize, string userId) 10 { 11 var userList = await MongodbHelper<User_Store>.FindListAsync(GetHostConfig("User_Store"), GetFilter<User_Store>("UserId", userId)); 12 13 if (userList.Any()) 14 { 15 var user = userList.FirstOrDefault(); 16 17 if (user.StoreList.Any()) 18 { 19 var list = new List<Core.Domain.Stores.Store>(); 20 foreach (var p in user.StoreList) 21 { 22 var resultModel = await _storeRepository.TableNoTracking.Where(k => k.Id == Guid.Parse(p)).FirstOrDefaultAsync(); 23 24 list.Add(resultModel); 25 } 26 27 return new PagedList<StoreCollectionOutput>(list.Select(p => new StoreCollectionOutput() { Id = p.Id.ToString(), StoreName = p.StoreName, LogoPath = p.StoreLogoPath }).AsQueryable(), pageIndex, pageSize); 28 } 29 } 30 return null; 31 }

1 private async Task CreateUserStoreAsync(string id, string userId) 2 { 3 var userList = await MongodbHelper<User_Store>.FindListAsync(GetHostConfig("User_Store"), GetFilter<User_Store>("UserId", userId)); 4 if (userList != null && userList.Any()) 5 { 6 var user = userList.FirstOrDefault(); 7 8 if (user.StoreList.Any(p => p.Equals(id))) 9 { 10 throw new ArgumentException("店铺已收藏"); 11 } 12 13 user.StoreList.Add(id); 14 15 await MongodbHelper<User_Store>.UpdateAsync(GetHostConfig("User_Store"), user, user.Id); 16 } 17 else 18 { 19 var model = new User_Store() 20 { 21 Id = Guid.NewGuid().ToString(), 22 UserId = userId, 23 StoreList = new List<string>() { id } 24 }; 25 26 await MongodbHelper<User_Store>.AddAsync(GetHostConfig("User_Store"), model); 27 } 28 }

1 private async Task CreateStoreUserAsync(string id, string userId) 2 { 3 var storeList = await MongodbHelper<Store_User>.FindListAsync(GetHostConfig("Store_User"), GetFilter<Store_User>("StoreId", id)); 4 if (storeList != null && storeList.Any()) 5 { 6 var store = storeList.FirstOrDefault(); 7 8 store.UserList.Add(userId); 9 10 await MongodbHelper<Store_User>.UpdateAsync(GetHostConfig("Store_User"), store, store.Id); 11 } 12 else 13 { 14 var model = new Store_User() 15 { 16 Id = Guid.NewGuid().ToString(), 17 StoreId = id, 18 UserList = new List<string>() { userId } 19 }; 20 21 await MongodbHelper<Store_User>.AddAsync(GetHostConfig("Store_User"), model); 22 } 23 }
配置mongodb地址

1 //Mongo地址 2 "MongodbHostConfig": { 3 "Connection": "mongodb://。。。。。。", 4 "DataBase": "。。。。。", 5 "Table": "。。。。。" 6 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律