This repository is a minimal .NET Web API that demonstrates CRUD operations against MongoDB for a games domain model.
It is designed for developers learning how to connect ASP.NET Core services to MongoDB Atlas or a local MongoDB instance.
- ASP.NET Core Web API with Swagger UI
- MongoDB .NET Driver integration through a dedicated service layer
- CRUD endpoints for a
Gameentity - Startup seed behavior that inserts sample games when the collection is empty
- Dev Container support with MongoDB Atlas Local for fast onboarding
- GitHub Actions CI with a local MongoDB service container
The API receives HTTP requests in GamesController, delegates data operations to GamesService, and persists documents in MongoDB.
- .NET 10 Web API
- MongoDB .NET Driver (
MongoDB.Driver) - Swagger / OpenAPI (
Swashbuckle.AspNetCore) - MongoDB Atlas Local (dev container),
mongo:latest(CI)
- .NET SDK 10+
- Docker 24+ (for local MongoDB or dev container workflows)
- Optional: MongoDB Atlas cluster connection string
- Start MongoDB locally (Docker example):
docker run --rm -d --name mongo-local -p 27017:27017 mongo:latest- Restore and run the API:
dotnet restore
dotnet run --urls http://localhost:5000- Verify service health and seeded data:
curl http://localhost:5000/healthz
curl http://localhost:5000/api/games- Open the API documentation UI:
http://localhost:5000/swagger/index.html
Expected outcomes:
- Health endpoint returns
Healthy GET /api/gamesreturns a JSON array containing sample records (for exampleCeleste,Hades)
- Open the repository in GitHub Codespaces (badge above) or VS Code Dev Containers.
- Wait for container setup to complete (
dotnet restoreruns automatically). - Run the API:
dotnet run --urls http://0.0.0.0:5000- Open:
- Swagger UI:
http://localhost:5000/swagger/index.html - Health endpoint:
http://localhost:5000/healthz
The app reads nested configuration via ASP.NET Core environment binding.
| Name | Required | Example | Description |
|---|---|---|---|
GamesDatabaseSettings__ConnectionString |
Yes | mongodb://localhost:27017 |
MongoDB connection string |
GamesDatabaseSettings__DatabaseName |
Yes | GamesDB |
MongoDB database name |
GamesDatabaseSettings__GamesCollectionName |
Yes | Games |
MongoDB collection name |
SEED_ON_STARTUP |
No | true |
Inserts default games if collection is empty (false disables) |
ASPNETCORE_ENVIRONMENT |
No | Development |
ASP.NET Core environment |
- MongoDB document mapping with BSON attributes (
Gamemodel) - Collection-level CRUD operations (
Find,InsertOne,ReplaceOne,DeleteOne) - Configuration-driven database and collection selection
- Standardized MongoDB client
appNamefor telemetry/observability
Relevant docs:
| Method | Path | Purpose |
|---|---|---|
GET |
/api/games |
List all games |
GET |
/api/games/{id} |
Get one game by ObjectId |
POST |
/api/games |
Create a game |
PUT |
/api/games/{id} |
Replace an existing game |
DELETE |
/api/games/{id} |
Delete a game |
GET |
/healthz |
Health check endpoint |
Controllers/GamesController.cs: API routes and HTTP responsesServices/GamesService.cs: MongoDB data access and seed helperModels/Game.cs: Document modelModels/GamesDatabaseSettings.cs: Configuration contractsModels/GameSeedData.cs: Default seed dataset.devcontainer/: Dev Container and Atlas Local configuration.github/workflows/ci.yml: CI build and smoke/integration checksEDD.md: MongoDB entity and index contract
CI runs on GitHub Actions and performs:
dotnet test tests/MongodbDotnetExample.Tests/MongodbDotnetExample.Tests.csprojdotnet restoredotnet build- API startup + health check
- CRUD smoke check against a local MongoDB service container
Run locally:
dotnet test tests/MongodbDotnetExample.Tests/MongodbDotnetExample.Tests.csproj
dotnet build- API starts but requests fail with MongoDB connection errors:
- Ensure MongoDB is running and reachable at
GamesDatabaseSettings__ConnectionString.
- Port binding errors on
5000:
- Run with a different port:
dotnet run --urls http://localhost:5050.
- Empty response from
/api/gamesafter first run:
- Confirm
SEED_ON_STARTUPis not set tofalse.
- Dev container builds but API cannot reach MongoDB:
- In shared network mode, use
mongodb://localhost:27017(notmongodb://mongodb:27017).
- Swagger does not load:
- Check
/healthzfirst; if healthy, verifyhttp://localhost:5000/swaggerand inspect server logs.
This project is licensed under the terms in LICENSE.
