using Model; using MongoDB.Bson; using MongoDB.Driver; namespace Repository { public class DataRepository { private static string COLLECTION_NAME = "data"; private string _connectionString = "mongodb://mongo:mongo@localhost:27017"; private string _databaseName = "mongo"; private IMongoCollection _dataCollection; private MongoClient _mongoClient; public DataRepository(string connectionString, string databaseName) { this._connectionString = connectionString; this._databaseName = databaseName; this._mongoClient = new MongoClient(this._connectionString); var db = this._mongoClient.GetDatabase(this._databaseName); this._dataCollection = db.GetCollection(COLLECTION_NAME); } public DataRepository() { this._mongoClient = new MongoClient(this._connectionString); var db = this._mongoClient.GetDatabase(this._databaseName); this._dataCollection = db.GetCollection(COLLECTION_NAME); } public async Task Save(DataModel data) { await this._dataCollection.InsertOneAsync(data); } public async Task FindById(string id) { var idFilter = Builders.Filter.Eq(data => data.id, id); return await this._dataCollection.Find(idFilter).FirstOrDefaultAsync(); } } }