MongoDbService 10.0.0

MongoDbService

Release to Nuget NuGet

MongoDbService is an open-source C# class library that provides a wrapper around the official MongoDB.Driver, simplifying MongoDB integration in .NET applications.

Features

  • Connection Tracking: Creates a ConnectionRecord collection that keeps track of compute instances connecting to your MongoDB instance
  • Standardized Configuration: Ensures uniform MongoDB configuration across all your projects
  • Simplified Integration: Abstracts connection management so you can focus on business logic

Requirements

  • .NET 8.0 or higher
  • MongoDB instance (local or cloud-based)

Installation

Install the NuGet package:

dotnet add package MongoDbService

Configuration

Add the following to your appsettings.json and update the values to match your MongoDB instance:

"MongoDbSettings": {
  "DatabaseName": "YourDatabaseName",
  "ConnectionString": "mongodb+srv://.........@gpcluster.0bulb.mongodb.net/myDatabase?retryWrites=true&w=majority"
}

Configuration Options:

  • DatabaseName (required): The name of your MongoDB database
  • ConnectionString (required): Your MongoDB connection string

Usage

Inject MongoService into your classes via dependency injection:

Example: Vehicle Management

1. Define your DTO:

using MongoDB.Bson.Serialization.Attributes;

namespace YourNameSpace
{
    public sealed class Vehicle
    {
        [BsonId]
        public required string Id { get; init; }
        public required string Name { get; set; }
    }
}

2. Create a handler class:

using MongoDB.Driver;
using MongoDbService;

namespace YourNameSpace
{
    public sealed class VehicleHandler
    {
        private readonly IMongoCollection<Vehicle> _vehicleCollection;

        public VehicleHandler(MongoService mongoService)
        {
            _vehicleCollection = mongoService.Database.GetCollection<Vehicle>(
                nameof(Vehicle), 
                new MongoCollectionSettings() 
                { 
                    ReadConcern = ReadConcern.Majority, 
                    WriteConcern = WriteConcern.WMajority 
                });
        }

        public async Task AddVehicle(string vehicleName)
        {
            await _vehicleCollection.InsertOneAsync(
                new Vehicle() 
                { 
                    Id = Guid.NewGuid().ToString(), 
                    Name = vehicleName 
                });
        }

        public async Task<DeleteResult> RemoveVehicle(string vehicleId)
        {
            return await _vehicleCollection.DeleteOneAsync(
                Builders<Vehicle>.Filter.Eq(v => v.Id, vehicleId));
        }
    }
}

Testing

The project includes integration tests that require a running MongoDB instance.

Running Tests Locally

  1. Ensure MongoDB is running on localhost:27017 (or set the MONGODB_CONNECTION_STRING environment variable)
  2. Run the tests:
dotnet test

CI/CD

The GitHub Actions workflow automatically runs tests against a MongoDB container on every release.

Contributing

We welcome contributions! If you find a bug or have an idea for improvement, please submit an issue or pull request on GitHub.

License

This project is licensed under the GNU General Public License v3.0.


Happy coding! 🚀🌐📚

No packages depend on MongoDbService.

Version Downloads Last updated
10.0.0 0 1/30/2026