- Install the package
dotnet add package 13angs.Dotnet.Mongo --version 0.0.1
- Register the config into DI container
MongodbConfig mongodbConfig = Configuration.GetMongodbConfig();
builder.Services.AddSingleton(mongodbConfig);
- Use the service
IMongoCollection<Blog?> _blog = MongodbCollectionService.GetCollection<Blog?>(Configuration);
- update the
appsetting.json
{
"MongodbConfig": {
"ConnectionString": "mongodb://root:P4ssw0rd@mongo-db:27017/",
"DatabaseName": "blog_db",
"CollectionName": "blogs"
}
}
- run the Mongodb server
cd Sample && docker compose up -d
- navigate to http://localhost:5255