MongoDBTools.cs 3.1 KB

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