To setup mongo db in Asp dot net web api you have to go to Mongo db website and make new database for your project.

Click on New project
And enter project name and create new project
After that go to Database and Build Database

Then choose free plane and create

Now create user Name for database and keep password and username remember

Click on Compass

Now copy connection string

Then Click on Done button
Now it time to setup on code
Go to appsettings.json
and past this code
"MonogoSetting": {
"ConnectionString": "mongodb+srv://Nitishroy7033:BCcs7ZMZDsTMiKFy@cluster0.96jk08d.mongodb.net/",
"DataBase": "UnivercityDb",
"StudentCollection": "Students",
"CollegeCollection": "Colleges"
},
Full Code Look like this
{
"MonogoSetting": {
"ConnectionString": "mongodb+srv://Nitishroy7033:BCcs7ZMZDsTMiKFy@cluster0.96jk08d.mongodb.net/",
"DataBase": "UnivercityDb",
"StudentCollection": "Students",
"CollegeCollection": "Colleges"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
5 and 6 like are the collection name where you want to save you data in separate collection
Now Install MongoDb package
Right Click on Project name -> and Manage Nuget Package for Solution

Search Mongodb and install

Now make folder like this

And Now make StudentModel
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Univercity.Models
{
public class Student
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}
}
Also Make Mongodb setting Model
namespace Univercity.DbContext
{
public class MongoDbSetting
{
public string ConnectionString { get; set; }
public string Database { get; set; }
public string StudentCollection { get; set; }
public string CollegeCollection { get; set; }
}
}
📁 Make a new file MongoDbSetting.cs
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Univercity.Models;
namespace Univercity.DbContext
{
public class MongoDbContext
{
private readonly IMongoCollection<Student> _student;
private readonly IMongoCollection<College> _college;
public MongoDbContext(IOptions<MongoDbSetting> mongoDBSettings)
{
MongoClientSettings clientSettings = MongoClientSettings.FromConnectionString(mongoDBSettings.Value.ConnectionString);
MongoClient client = new MongoClient(clientSettings);
IMongoDatabase database = client.GetDatabase(mongoDBSettings.Value.Database);
_student = database.GetCollection<Student>(mongoDBSettings.Value.StudentCollection);
_college = database.GetCollection<College>(mongoDBSettings.Value.CollegeCollection);
}
public IMongoCollection<Student> StudentDb => _student;
public IMongoCollection<College> CollegeDb => _college;
}
}
C#Now the mongo db setup is completed
One Last Step is Remaining that is Add Mongodb Setting middleWare
Go To Program.cs
using Univercity.DbContext;
using Univercity.Services;
using Univercity.Services.Interfaces;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Register MongoDbContext with configuration
builder.Services.Configure<MongoDbSetting>(builder.Configuration.GetSection("MonogoSetting"));
builder.Services.AddScoped<MongoDbContext>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
C#Add this line
Thanks you ❤️