MongoDB + PostgreSQL Injection
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB
|
||||
{
|
||||
public class MongoDbService
|
||||
{
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
public MongoDbService(string connectionString)
|
||||
{
|
||||
var url = new MongoUrl(connectionString);
|
||||
var client = new MongoClient(url);
|
||||
_database = client.GetDatabase(url.DatabaseName);
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>(string collectionName)
|
||||
{
|
||||
return _database.GetCollection<T>(collectionName);
|
||||
}
|
||||
|
||||
public async Task<List<T>> FindAsync<T>(string collectionName, List<(string FieldName, string Value)> criteria)
|
||||
{
|
||||
var collection = _database.GetCollection<T>(collectionName);
|
||||
|
||||
var filters = new List<FilterDefinition<T>>();
|
||||
foreach (var (FieldName, Value) in criteria)
|
||||
{
|
||||
filters.Add(Builders<T>.Filter.Eq(FieldName, Value));
|
||||
}
|
||||
|
||||
var combinedFilter = Builders<T>.Filter.And(filters);
|
||||
|
||||
return await collection.Find(combinedFilter).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user