Database MongoDB C#

MongoDB with C# and .NET 9 – Complete Guide: From Zero to REST APIs

MongoDB with C# and .NET 9: CRUD, embedded documents, advanced filters, async/await, and a full REST API with Minimal API and the official driver.

Course description

MongoDB with C# and .NET 9: from the official driver to a complete REST API

MongoDB is one of the most widely used NoSQL databases in the world, and working with it from C# and .NET 9 is much more straightforward than you might think. In this course we build everything from the start: setup with MongoDB Compass, class mapping, full CRUD, embedded documents, and then a Minimal REST API that exposes everything over HTTP. No shortcuts — every piece is built and truly understood.

What You'll Learn

  • Set up MongoDB locally with MongoDB Compass and connect it to a .NET 9 project
  • Map a C# class as a MongoDB document with attributes from the official driver
  • Save documents to MongoDB from C# with insertion and database connection
  • Build advanced filters with Find() and FilterDefinitionBuilder for precise queries
  • Delete documents with filters and verify the result with DeletedCount
  • Update documents with ReplaceOne: full replacement with filter
  • Manage embedded documents (nested): mapping, insertion, and search with complex filters
  • Create a Minimal REST API in .NET 9 connected to MongoDB
  • Implement insert and search endpoints with async/await for scalability
  • Complete the REST CRUD: endpoints for deleting and updating data

The Document Database for the .NET World

MongoDB adapts well to flexible, hierarchical, and rapidly evolving data structures — situations where a fixed SQL schema would be a constraint. Integrating it into .NET 9 with the official driver is straightforward, but doing it well requires understanding mapping, filters, and embedded object handling. This course gives you a solid foundation to build .NET applications with MongoDB professionally.

Who This Course Is For

Ideal for C# and .NET developers who want to add MongoDB to their stack. Familiarity with C#, Minimal APIs, and basic REST API concepts is required. No prior MongoDB experience is needed.

Lessons list

Let's configure MongoDB locally with MongoDB Compass, explore the document-based structure, and connect the database to a .NET 9 project by installing the official C# driver via NuGet.

Guarda lezione

Let's create the .NET 9 console project, install the MongoDB driver, and map the first C# class as a MongoDB document using BsonId and BsonElement attributes to control field names.

Guarda lezione

Let's save our first object to MongoDB from C#: I'll configure the connection string, obtain a reference to the collection, and use InsertOne to insert a document, verifying the result in Compass.

Guarda lezione

Let's see how to search documents in MongoDB from C# using Find(), and build advanced filters with FilterDefinitionBuilder: equality filters, numeric ranges, and combinations with And and Or logical operators.

Guarda lezione

Let's delete documents from MongoDB in C# using precise filters with DeleteOne and DeleteMany, and verify the result with the DeletedCount property returned by the driver.

Guarda lezione

Let's update documents in MongoDB from C# with ReplaceOne: I'll build the selection filter, modify the C# object, and replace the entire document in the collection, checking the outcome with ModifiedCount.

Guarda lezione

I'll show you how to map embedded classes in C# to represent nested documents in MongoDB, using driver attributes and understanding how the driver serialises child class properties.

Guarda lezione

Let's insert documents with embedded objects into MongoDB from C# and build advanced filters to query nested fields, using dot notation and the official driver's builder classes.

Guarda lezione

Let's start building the Minimal REST API in .NET 9 connected to MongoDB: I'll configure the client as a service, register dependencies in the DI container, and map the first HTTP endpoints.

Guarda lezione

Let's implement the insert and search endpoints in the .NET 9 Minimal REST API with MongoDB: I'll handle JSON body deserialisation, invoke the driver, and return correct HTTP responses.

Guarda lezione

Let's make all MongoDB methods in the REST API asynchronous with async/await using InsertOneAsync and FindAsync, and add a targeted search endpoint to retrieve a document by its ID.

Guarda lezione

Let's complete the CRUD for the .NET 9 Minimal REST API: I'll add DELETE and PUT endpoints to remove and update MongoDB documents, handling the correct HTTP response codes for each case.

Guarda lezione
Giovanni Pace

Giovanni Pace

Ingegnere Informatico · Pratola Peligna

  • 107 Courses
About the teacher

Sono un ingegnere informatico con oltre 15 anni di esperienza nel settore ICT. Mi occupo di sviluppo software, cybersecurity, data science e intelligenza artificiale, con particolare attenzione all’innovazione e all’automazione dei processi aziendali. Da anni svolgo anche attività di docenza e formazione, creando percorsi pratici e coinvolgenti su programmazione, DevOps, AI e sicurezza informatica.

This course includes

  • Lessons 12
  • Duration 2h 39m
  • Difficulty intermedio
  • Language it
  • Certificate No

Recently viewed