namespace FreeSR.Database.Mongo { using MongoDB.Driver; using System.Linq.Expressions; public class SRMongoDatabase : ISRDatabase where T : class { protected readonly IMongoCollection _collection; public SRMongoDatabase(IMongoDatabase database, string collectionName) { _collection = database.GetCollection(collectionName); } public async Task Insert(T document) { await _collection.InsertOneAsync(document); } public async Task InsertMany(IEnumerable documents) { await _collection.InsertManyAsync(documents); } public async Task> Find(Expression> filter) { var result = await _collection.FindAsync(filter); return await result.ToListAsync(); } public async Task FindOne(Expression> filter) { var result = await _collection.FindAsync(filter); return await result.FirstOrDefaultAsync(); } public async Task Update(Expression> filter, T updatedDocument) { await _collection.ReplaceOneAsync(filter, updatedDocument); } public async Task Delete(Expression> filter) { await _collection.DeleteOneAsync(filter); } public async Task Count() { return await _collection.CountDocumentsAsync(Builders.Filter.Empty); } public async Task FindMax(Expression> fieldSelector) { var sortDefinition = Builders.Sort.Descending(fieldSelector); return await _collection.Find(Builders.Filter.Empty).Sort(sortDefinition).FirstOrDefaultAsync(); } } }