using MongoDB.Bson; using MongoDB.Driver; using System.Collections.Generic; using MongoDB.Bson.Serialization; using MongoToSql.Models; using System.Linq.Expressions; using System; namespace MongoToSql.Repository { public class WindsRepository { protected static IMongoClient _client; protected static IMongoDatabase _database; protected IMongoCollection _collection; public WindsRepository() { _client = new MongoClient("mongodb://localhost:27017/"); _database = _client.GetDatabase("Homework"); _collection = _database.GetCollection("WindData"); } public WindsModel InsertPost(WindsModel contact) { this._collection.InsertOneAsync(contact); return this.Get(contact._id.ToString()); } public List SelectAll() { var query = this._collection.Find(new BsonDocument()).ToListAsync(); return query.Result; } public List Filter(string jsonQuery) { var queryDoc = new QueryDocument(BsonSerializer.Deserialize(jsonQuery)); return _collection.Find(queryDoc).ToList(); } public WindsModel Get(string id) { return this._collection.Find(new BsonDocument { { "_id", new ObjectId(id) } }).FirstAsync().Result; } public WindsModel UpdatePost(string id, WindsModel windsmodel) { windsmodel._id = new ObjectId(id); var filter = Builders.Filter.Eq(s => s._id, windsmodel._id); this._collection.ReplaceOneAsync(filter, windsmodel); return this.Get(id); } public List Query(Expression> expression) { return _collection.Find(expression).ToList(); } } }