| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using Business.Core.Attributes;
- using Microsoft.Extensions.Options;
- using MongoDB.Bson;
- using MongoDB.Driver;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Business.Core.MongoDBHelper
- {
- /// <summary>
- /// MongoDB实现
- /// </summary>
- public class MongoDBTools<T> : IMongoDB<T> where T : class
- {
- public readonly IMongoCollection<T> mongoCollection;
- public IOptionsSnapshot<Config> _config;
- /// <summary>
- /// MongoDB帮助类
- /// </summary>
- /// <param name="config"></param>
- /// <exception cref="NotImplementedException"></exception>
- public MongoDBTools(IOptionsSnapshot<Config> config)
- {
- _config = config;
- //数据库链接
- MongoClient client = new MongoClient(config.Value.connectstring);
- CollectionNameAttribute collectonName = typeof(T).GetCustomAttributes(typeof(CollectionNameAttribute), true).FirstOrDefault() as CollectionNameAttribute;
- if (collectonName == null)
- {
- throw new NotImplementedException("请配置Attribute属性!");
- }
- //数据库
- var database = client.GetDatabase(collectonName.DatabaseName);
- //表名
- mongoCollection = database.GetCollection<T>(collectonName.CollectionName);
- }
- public Task<long> BulkInsert(List<T> documents)
- {
- throw new NotImplementedException();
- }
- public Task<List<T>> GetAll()
- {
- throw new NotImplementedException();
- }
- public Task<T> GetOneByID(long id)
- {
- throw new NotImplementedException();
- }
- public Task Inc(long id)
- {
- throw new NotImplementedException();
- }
- public Task InsertMany(List<T> documents)
- {
- throw new NotImplementedException();
- }
- public Task InsertOne(T document)
- {
- return mongoCollection.InsertOneAsync(document);
- }
- public Task<UpdateResult> UpdateMultiFields(T document, long id)
- {
- throw new NotImplementedException();
- }
- public Task<UpdateResult> UpdateOne(string name, long id)
- {
- throw new NotImplementedException();
- }
- }
- }
|