MongoDBTools.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Business.Core.Attributes;
  2. using Microsoft.Extensions.Options;
  3. using MongoDB.Bson;
  4. using MongoDB.Driver;
  5. using MongoDB.Driver.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Volo.Abp.Domain.Entities;
  12. namespace Business.Core.MongoDBHelper
  13. {
  14. /// <summary>
  15. /// MongoDB帮助类
  16. /// </summary>
  17. public class MongoDBTools<T> : IMongoDB<T> where T : Entity<long>
  18. {
  19. public readonly IMongoCollection<T> mongoCollection;
  20. public IOptionsSnapshot<Config> _config;
  21. /// <summary>
  22. /// MongoDB链接
  23. /// </summary>
  24. /// <param name="config"></param>
  25. /// <exception cref="NotImplementedException"></exception>
  26. public MongoDBTools(IOptionsSnapshot<Config> config)
  27. {
  28. _config = config;
  29. //数据库链接
  30. MongoClient client = new MongoClient(config.Value.connectstring);
  31. CollectionNameAttribute collectonName = typeof(T).GetCustomAttributes(typeof(CollectionNameAttribute), true).FirstOrDefault() as CollectionNameAttribute;
  32. if (collectonName == null)
  33. {
  34. throw new NotImplementedException("请配置Attribute属性!");
  35. }
  36. //数据库
  37. var database = client.GetDatabase(collectonName.DatabaseName);
  38. //表名
  39. mongoCollection = database.GetCollection<T>(collectonName.CollectionName);
  40. }
  41. /// <summary>
  42. /// 插入一条数据
  43. /// </summary>
  44. /// <param name="document"></param>
  45. /// <returns></returns>
  46. public Task InsertOne(T document)
  47. {
  48. return mongoCollection.InsertOneAsync(document);
  49. }
  50. /// <summary>
  51. /// 插入多条数据
  52. /// </summary>
  53. /// <param name="documents"></param>
  54. /// <returns></returns>
  55. /// <exception cref="NotImplementedException"></exception>
  56. public Task InsertMany(List<T> documents)
  57. {
  58. return mongoCollection.InsertManyAsync(documents,new InsertManyOptions() { IsOrdered = false});
  59. }
  60. /// <summary>
  61. /// 更新一条数据
  62. /// </summary>
  63. /// <param name="documents"></param>
  64. /// <param name="id"></param>
  65. /// <returns></returns>
  66. public Task<ReplaceOneResult> UpdateOne(T documents,long id)
  67. {
  68. return mongoCollection.ReplaceOneAsync(Builders<T>.Filter.Eq(p=>p.Id, id), documents);
  69. }
  70. /// <summary>
  71. /// 获取所有数据
  72. /// </summary>
  73. /// <returns></returns>
  74. /// <exception cref="NotImplementedException"></exception>
  75. public Task<List<T>> GetAll()
  76. {
  77. return mongoCollection.AsQueryable<T>().ToListAsync();
  78. }
  79. /// <summary>
  80. /// 跟据Id获取数据
  81. /// </summary>
  82. /// <param name="id"></param>
  83. /// <returns></returns>
  84. /// <exception cref="NotImplementedException"></exception>
  85. public Task<T> GetOneByID(long id)
  86. {
  87. return mongoCollection.Find(p => p.Id == id).FirstOrDefaultAsync();
  88. }
  89. }
  90. }