When you think about database providers for ASP NET Core apps, you probably think about Entity Framework Core (EF Core), which handles interacting with SQL databases. But what about the NoSQL options? A popular option for NoSQL is MongoDB. So in this article, we're going to learn how to create a simple ASP NET Core CRUD API using MongoDB as the database provider. I have created a for this project, so please feel free to clone it . GitHub repo here Start by creating an ASP NET Core Web API project and then install the MongoDb C# Driver NuGet package: MongoDB.Driver Actually creating the MongoDB database is outside of the scope of this tutorial, so I will assume that you already have a MongoDB database, either locally or hosted on some provider. I personally used Docker to host my database locally, so you can see how I did that by seeing my docker-compose.dcproj and mongo-init.js files in my . GitHub repo Next, we need to enter the connection string for the MongoDB database in our appsettings.json: : { : } "ConnectionStrings" "MongoDb" "mongodb://MyUser:MyPassword@mongodb:27017/MyDb" And then use this connection string to set up the dependency injection (DI) of IMongoClient from the MongoDB.Driver package. In the ConfigureServices method of Startup.cs, add the following line to create IMongoClient as a : singleton service services.AddSingleton<IMongoClient, MongoClient>(sp => MongoClient(Configuration.GetConnectionString( ))); new "MongoDb" We're going to create a basic CRUD application with a database containing the details of different cars. So let's get started by creating a Models/Car.cs class: { ObjectId Id { ; ; } Make { ; ; } Model { ; ; } TopSpeed { ; ; } } public class Car public get set public string get set public string get set public int get set The ObjectId is a data type from the MongoDB driver that stores the unique identifier of the object within the database. In this project, we'll be using the to access and modify our data. So we'll start by creating the interface for our repository, in Repositories/ICarRepository.cs: Repository pattern { ; ; Task<IEnumerable<Car>> Get(); Task<IEnumerable<Car>> GetByMake( make); ; ; } public interface ICarRepository // Create Task<ObjectId> ( ) Create Car car // Read Task<Car> ( ) Get ObjectId objectId string // Update Task< > ( ) bool Update ObjectId objectId, Car car // Delete Task< > ( ) bool Delete ObjectId objectId We'll then go through creating the Repositories/CarRepository.cs class step by step, starting with the constructor and private fields: : { IMongoCollection<Car> _cars; { database = client.GetDatabase( ); collection = database.GetCollection<Car>( (Car)); _cars = collection; } } public class CarRepository ICarRepository private readonly ( ) public CarRepository IMongoClient client var "MyDb" var nameof Here we are passing an instance of IMongoClient (which we set up for DI earlier) to our repository. From that, we are getting the database and the specific collection we want - in this case the Cars collection - and assigning that collection to the field _cars to use later. Note that, if the collection doesn't already exist, the client will automatically create it when we first try to access it, so we don't need to worry about manually setting up collections. Next, we move on to the Create method: { _cars.InsertOneAsync(car); car.Id; } Task<ObjectId> ( ) public async Create Car car await return This one is fairly self-explanatory - we simply insert the car object into the collection and return the ObjectId that is created when the object is inserted into the database. Next are the 3 read methods: { filter = Builders<Car>.Filter.Eq(c => c.Id, objectId); car = _cars.Find(filter).FirstOrDefaultAsync(); car; } Task<IEnumerable<Car>> Get() { cars = _cars.Find(_ => ).ToListAsync(); cars; } Task<IEnumerable<Car>> GetByMake( make) { filter = Builders<Car>.Filter.Eq(c => c.Make, make); cars = _cars.Find(filter).ToListAsync(); cars; } Task<Car> ( ) public Get ObjectId objectId var var return public async var await true return public async string var var await return These all follow a similar pattern. First, we create a filter object by which to filter the collection and then we ask the client to return either a single object or a list of objects based on our filter. In the case of the Get() method that returns all car objects, we created a simple filter that will always return true. The update method is perhaps the most fiddly of the CRUD operations, mostly because the client insists that each field that needs to be updated must be manually set: { filter = Builders<Car>.Filter.Eq(c => c.Id, objectId); update = Builders<Car>.Update .Set(c => c.Make, car.Make) .Set(c => c.Model, car.Model) .Set(c => c.TopSpeed, car.TopSpeed); result = _cars.UpdateOneAsync(filter, update); result.ModifiedCount == ; } Task< > ( ) public async bool Update ObjectId objectId, Car car var var var await return 1 Here we must first create a filter object to only select the object that we wish to update. Note that it is possible to update several objects at once if your filter selects multiple objects and you use the UpdateMany method. We then create an update object, which sets each of the fields in the database to those that we have passed into the method. We then tell the client to actually perform the update operation with the UpdateOneAsync method. Finally, we have the delete method. It should start to look somewhat familiar by now - we create a filter to select only the record that we want, then use the DeleteOneAsync method to actually delete it. { filter = Builders<Car>.Filter.Eq(c => c.Id, objectId); result = _cars.DeleteOneAsync(filter); result.DeletedCount == ; } Task< > ( ) public async bool Delete ObjectId objectId var var await return 1 And that's it for our repository. We also need to add this to the ConfigureServices method of Startup.cs in order to configure the DI: services.AddTransient<ICarRepository, CarRepository>(); The last thing that we need to do is wire it up to a Controller class so that we can access it as an API. Since this will be very boilerplate ASP NET Core controller code, I'll simply present the whole controller class (Controllers/CarsController.cs): [ ] [ ] : { ICarRepository _carRepository; { _carRepository = carRepository; } [ ] { id = _carRepository.Create(car); JsonResult(id.ToString()); } [ ] { car = _carRepository.Get(ObjectId.Parse(id)); JsonResult(car); } [ ] { cars = _carRepository.Get(); JsonResult(cars); } [ ] { cars = _carRepository.GetByMake(make); JsonResult(cars); } [ ] { result = _carRepository.Update(ObjectId.Parse(id), car); JsonResult(result); } [ ] { result = _carRepository.Delete(ObjectId.Parse(id)); JsonResult(result); } } ApiController Route( ) "[controller]" public class CarsController ControllerBase private readonly ( ) public CarsController ICarRepository carRepository HttpPost Task<IActionResult> ( ) public async Create Car car var await return new HttpGet( ) "{id}" Task<IActionResult> ( ) public async Get id string var await return new HttpGet Task<IActionResult> ( ) public async Get var await return new HttpGet( ) "ByMake/{make}" Task<IActionResult> ( ) public async GetByMake make string var await return new HttpPut( ) "{id}" Task<IActionResult> ( ) public async Update id, Car car string var await return new HttpDelete( ) "{id}" Task<IActionResult> ( ) public async Delete id string var await return new And that's it! You can now run the application and calls the standard CRUD API endpoints to create, read, update, and Delete Car records using MongoDB as the database. Conclusion In this article, we have learned how to use the MongoDB C# Driver to create ASP NET Core API powered by a MongoDB database. Don't forget to check out the for this project . Github repo here Since it's almost Christmas, you could also check out my post that was featured on Hashnode recently. Top Christmas Gifts To Buy A Developer I post mostly about full stack .NET and Vue web development. To make sure that you don't miss out on any posts, please follow this blog and . If you found this post helpful, please like it and share it. You can also find me on . subscribe to my newsletter Twitter Previously published at https://samwalpole.com/how-to-use-a-mongodb-database-in-a-asp-net-core-web-api-application