Archived
1
0
Fork 0
forked from Moux23333/FreeSR
This repository has been archived on 2024-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
FreeSR/FreeSR.Database/Mongo/SRMongoDatabase.cs
2024-01-27 21:06:07 +08:00

58 lines
1.8 KiB
C#

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