Skip to content

pieringro/SimpleMongoDBWrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleMongoDBWrapper

.Net Core Class library. Simple wrapper for MongoDB

Get started

  • Set configuration file (appsettings.json in root of your project)
"ConnectionStrings": {
  "DatabaseName": "MyDb",
  "MyDb": "mongodb://localhost:27017/database"
},
"ItemsPerPage" : 10
  • Initialize DbContext
var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json");
var config = builder.Build();
DBContext.GetInstance(config);
  • Create a model class that extends BaseCollection
using SimpleMongoDBWrapper;

public class TestModel : BaseCollection {
    public string data { get; set; }
}

BaseCollection contains a string field "Id", declared for mongo as BsonId

  • Read
var rep = new Repository<TestModel>();
var list = await rep.GetAll();
  • Insert
var rep = new Repository<TestModel>();
rep.InsertOne(new TestModel() {
    Id = "5ccedb8ae69af8035cab128d",
    data = "Hello world!"
}).Wait();
  • Update
TestModel model = new TestModel() {
    data = "Hello world! Edited!!!"
};
var rep = new Repository<TestModel>();
var result = rep.UpdateOne("5ccedb8ae69af8035cab128d", model).Result;
  • Delete
var rep = new Repository<TestModel>();
var result = rep.DeleteOne("5ccedb8ae69af8035cab128d").Result;

About

.Net Core Class library. Simple wrapper for MongoDB

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages