MongoDBTools.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Linq.Expressions;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Volo.Abp.Domain.Entities;
  13. namespace Business.Core.MongoDBHelper
  14. {
  15. /// <summary>
  16. /// MongoDB帮助类
  17. /// </summary>
  18. public class MongoDBTools<T> : IMongoDB<T> where T : Entity<long>
  19. {
  20. public readonly IMongoCollection<T> mongoCollection;
  21. public IOptionsSnapshot<Config> _config;
  22. /// <summary>
  23. /// MongoDB链接
  24. /// </summary>
  25. /// <param name="config"></param>
  26. /// <exception cref="NotImplementedException"></exception>
  27. public MongoDBTools(IOptionsSnapshot<Config> config)
  28. {
  29. _config = config;
  30. //数据库链接
  31. MongoClient client = new MongoClient(config.Value.connectstring);
  32. CollectionNameAttribute collectonName = typeof(T).GetCustomAttributes(typeof(CollectionNameAttribute), true).FirstOrDefault() as CollectionNameAttribute;
  33. if (collectonName == null)
  34. {
  35. throw new NotImplementedException("请配置Attribute属性!");
  36. }
  37. //数据库
  38. var database = client.GetDatabase(collectonName.DatabaseName);
  39. //表名
  40. mongoCollection = database.GetCollection<T>(collectonName.CollectionName);
  41. }
  42. /// <summary>
  43. /// 插入一条数据
  44. /// </summary>
  45. /// <param name="document"></param>
  46. /// <returns></returns>
  47. public Task InsertOne(T document)
  48. {
  49. return mongoCollection.InsertOneAsync(document);
  50. }
  51. /// <summary>
  52. /// 插入多条数据
  53. /// </summary>
  54. /// <param name="documents"></param>
  55. /// <returns></returns>
  56. /// <exception cref="NotImplementedException"></exception>
  57. public Task InsertMany(List<T> documents)
  58. {
  59. return mongoCollection.InsertManyAsync(documents,new InsertManyOptions() { IsOrdered = false});
  60. }
  61. /// <summary>
  62. /// 更新一条数据
  63. /// </summary>
  64. /// <param name="documents"></param>
  65. /// <param name="id"></param>
  66. /// <returns></returns>
  67. public Task<ReplaceOneResult> UpdateOne(T documents,long id)
  68. {
  69. return mongoCollection.ReplaceOneAsync(Builders<T>.Filter.Eq(p=>p.Id, id), documents);
  70. }
  71. /// <summary>
  72. /// 获取所有数据
  73. /// </summary>
  74. /// <returns></returns>
  75. /// <exception cref="NotImplementedException"></exception>
  76. public Task<List<T>> GetAll()
  77. {
  78. return mongoCollection.AsQueryable<T>().ToListAsync();
  79. }
  80. /// <summary>
  81. /// 跟据Id获取数据
  82. /// </summary>
  83. /// <param name="id"></param>
  84. /// <returns></returns>
  85. /// <exception cref="NotImplementedException"></exception>
  86. public Task<T> GetOneByID(long id)
  87. {
  88. return mongoCollection.Find(p => p.Id == id).FirstOrDefaultAsync();
  89. }
  90. /// <summary>
  91. /// 根据条件获取数据
  92. /// </summary>
  93. /// <returns></returns>
  94. public Task<List<T>> GetManyByCondition(Expression<Func<T, bool>> filter)
  95. {
  96. return mongoCollection.Find(filter).ToListAsync();
  97. }
  98. /// <summary>
  99. /// 根据条件获取数据
  100. /// </summary>
  101. /// <returns></returns>
  102. public Task<List<T>> GetManyByIds(FilterDefinition<T> filter)
  103. {
  104. return mongoCollection.Find(filter).ToListAsync();
  105. }
  106. /// <summary>
  107. /// 根据id删除数据
  108. /// </summary>
  109. /// <returns></returns>
  110. public Task DeleteByIds(long id)
  111. {
  112. return mongoCollection.DeleteOneAsync(p => p.Id == id);
  113. }
  114. /// <summary>
  115. /// 根据条件删除数据
  116. /// </summary>
  117. /// <returns></returns>
  118. public Task DeleteManyByIds(FilterDefinition<T> filter)
  119. {
  120. return mongoCollection.DeleteManyAsync(filter);
  121. }
  122. /// <summary>
  123. /// 根据条件删除数据
  124. /// </summary>
  125. /// <returns></returns>
  126. public Task DeleteManyByCondition(Expression<Func<T, bool>> filter)
  127. {
  128. return mongoCollection.DeleteManyAsync<T>(filter);
  129. }
  130. }
  131. }