This easy to use library implements the repository pattern on top of Official MongoDB C# driver. This project works .NET Core 3.1 and greater
You can connect DB with MongoRepositoryPattern
also more than one methods
MongoRepositoryPattern Connector = new MongoRepositoryPattern(new MongoDataContext());
MongoRepositoryPattern Connector = new MongoRepositoryPattern(new MongoDataContext("root", "*******", "admin.mlab.com", "27017", "admin", false));
MongoRepositoryPattern Connector = new MongoRepositoryPattern(new MongoDataContext("root:*******[email protected]:27017", "admin"));
Collection
string CollectionName<Model>() where Model : Base, new();
IMongoCollection<Model> Collection<Model>(string collectionName = null) where Model : Base, new();
With Async
Task<ISingleResult<Model>> InsertOneAsync<Model>(Model model, string collectionName = null) where Model : Base, new();
Task<IListResult<Model>> InsertManyAsync<Model>(List<Model> model, string collectionName = null) where Model : Base, new();
Task<ISingleResult<long>> CountDocumentsAsync<Model>(string collectionName = null) where Model : Base, new();
Task<ISingleResult<long>> CountDocumentsAsync<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
Task<ISingleResult<DeleteResult>> DeleteOneAsync<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
Task<ISingleResult<DeleteResult>> DeleteManyAsync<Model>(string collectionName = null) where Model : Base, new();
Task<ISingleResult<DeleteResult>> DeleteManyAsync<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
Task<ISingleResult<bool>> ExistAsync<Model>(Expression<Func<Model, bool>> predicate, string collectionName) where Model : Base, new();
Task<ISingleResult<Model>> GetAsync<Model>(Expression<Func<Model, bool>> predicate, string collectionName) where Model : Base, new();
Task<IListResult<Model>> GetListAsync<Model>(string collectionName = null) where Model : Base, new();
Task<IListResult<Model>> GetListAsync<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
Task<IListResult<Model>> GetListAsync<Model>(Expression<Func<Model, bool>> predicate, int limit, string collectionName = null) where Model : Base, new();
Task<IListResult<Model>> GetListAsync<Model>(Expression<Func<Model, bool>> predicate, string sortField, bool descending, string collectionName = null) where Model : Base, ne();
Task<IListResult<Model>> GetListAsync<Model>(Expression<Func<Model, bool>> predicate, string sortField, bool descending, int limit, string collectionName = null) where Model: Base, new();
Task<IListResult<Model>> GetListAsync<Model>(Expression<Func<Model, bool>> predicate, string sortField, bool descending, int limit, int skip, string collectionName = null)where Model : Base, new();
Task<ISingleResult<string>> IndexesCreateOneAsync<Model>(string text, string collectionName = null) where Model : Base, new();
Task<ISingleResult<Model>> GetSingleAsync<Model>(string id, string collectionName = null) where Model : Base, new();
Task<ISingleResult<Model>> SaveAsync<Model>(Model model, string collectionName = null) where Model : Base, new();
Task<ISingleResult<UpdateResult>> UpdateAsync<Model>(FilterDefinition<Model> filter, UpdateDefinition<Model> update, bool multi = false, string collectionName = null) where Model : Base, new();
Without Async
ISingleResult<Model> InsertOne<Model>(Model model, string collectionName = null) where Model : Base, new();
IListResult<Model> InsertMany<Model>(List<Model> model, string collectionName = null) where Model : Base, new();
ISingleResult<long> CountDocuments<Model>(string collectionName = null) where Model : Base, new();
ISingleResult<long> CountDocuments<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
ISingleResult<DeleteResult> DeleteOne<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
ISingleResult<DeleteResult> DeleteMany<Model>(string collectionName = null) where Model : Base, new();
ISingleResult<DeleteResult> DeleteMany<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
ISingleResult<bool> Exist<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
ISingleResult<Model> Get<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
IListResult<Model> GetList<Model>(string collectionName = null) where Model : Base, new();
IListResult<Model> GetList<Model>(Expression<Func<Model, bool>> predicate, string collectionName = null) where Model : Base, new();
IListResult<Model> GetList<Model>(Expression<Func<Model, bool>> predicate, int limit, string collectionName = null) where Model : Base, new();
IListResult<Model> GetList<Model>(Expression<Func<Model, bool>> predicate, string sortField, bool descending, string collectionName = null) where Model : Base, new();
IListResult<Model> GetList<Model>(Expression<Func<Model, bool>> predicate, string sortField, bool descending, int limit, string collectionName = null) where Model : Base, ne();
IListResult<Model> GetList<Model>(Expression<Func<Model, bool>> predicate, string sortField, bool descending, int limit, int skip, string collectionName = null) where Model :Base, new();
ISingleResult<string> IndexesCreateOne<Model>(string text, string collectionName = null) where Model : Base, new();
ISingleResult<Model> GetSingle<Model>(string id, string collectionName = null) where Model : Base, new();
ISingleResult<Model> Save<Model>(Model model, string collectionName = null) where Model : Base, new();
ISingleResult<UpdateResult> Update<Model>(FilterDefinition<Model> filter, UpdateDefinition<Model> update, bool multi = false, string collectionName = null) where Model : Base, new();
[HttpPost]
public IActionResult Save()
{
MongoRepositoryPattern Connector = new MongoRepositoryPattern(new MongoDataContext());
Product Model = new Product()
{
Status = Status.Active,
Name = "iPhone 11 - 64 GB"
};
ISingleResult<Product> SaveResult = Connector.Save<Product>(Model);
return Json(SaveResult.DidError ?
new { Status = false, Msg = SaveResult.Message, Model = default(Product) } :
new { Status = true, Msg = "Success", Model = SaveResult.Model });
}
}