MongoGenericRepository.HealthChecks 11.0.0

dotnet add package MongoGenericRepository.HealthChecks --version 11.0.0
                    
NuGet\Install-Package MongoGenericRepository.HealthChecks -Version 11.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MongoGenericRepository.HealthChecks" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MongoGenericRepository.HealthChecks" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MongoGenericRepository.HealthChecks" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MongoGenericRepository.HealthChecks --version 11.0.0
                    
#r "nuget: MongoGenericRepository.HealthChecks, 11.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MongoGenericRepository.HealthChecks@11.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MongoGenericRepository.HealthChecks&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MongoGenericRepository.HealthChecks&version=11.0.0
                    
Install as a Cake Tool

<p align="center"> <img src="mongorepository_logo.svg" alt="MongoRepository" width="128" /> </p>

MongoRepository

Generic, extensible CRUD repository for MongoDB — targeting .NET 8, 9, and 10.

NuGet MongoGenericRepository NuGet HealthChecks CI License: MIT Docs

Package Description
MongoGenericRepository Generic read/write repository with read/write separation support
MongoGenericRepository.HealthChecks ASP.NET Core health checks for MongoDB connections

Installation

dotnet add package MongoGenericRepository

# Optional: health checks
dotnet add package MongoGenericRepository.HealthChecks

Quick Start

Configure your connection strings:

{
  "MongoDbOptions": {
    "ReadWriteConnection": "mongodb://localhost:27017/MyDatabase",
    "ReadOnlyConnection": "mongodb://secondary:27017/MyDatabase?readPreference=secondaryPreferred"
  }
}

Define an entity:

[EntityDatabase("MyDatabase")]
[EntityCollection("Products")]
public class Product : IEntity<string>
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}

Create a repository:

public interface IProductRepository : IReadWriteRepository<Product, string>
{
    Task<List<Product>> GetByCategory(string category);
}

public class ProductRepository : ReadWriteRepository<Product, string>, IProductRepository
{
    public ProductRepository(IOptions<MongoDbOptions> mongoOptions) : base(mongoOptions) { }

    public async Task<List<Product>> GetByCategory(string category)
    {
        var filter = Builders<Product>.Filter.Eq(p => p.Category, category);
        return await Collection.Find(filter).ToListAsync();
    }
}

Register services:

builder.Services.Configure<MongoDbOptions>(
    builder.Configuration.GetSection("MongoDbOptions"));
builder.Services.AddScoped<IProductRepository, ProductRepository>();

Read/Write Separation

MongoRepository supports separate connections for read and write operations — useful for directing read traffic to secondary nodes in replica sets. If ReadOnlyConnection is not set, all operations fall back to ReadWriteConnection.

// Read-only repository for services that only need to query data
public class ProductReader : ReadOnlyDataRepository<Product, string>, IProductReader
{
    public ProductReader(IOptions<MongoDbOptions> mongoOptions) : base(mongoOptions) { }
}

Repository API

Method Description
Get(id) Get entity by ID
Get(ids) Get multiple entities by IDs
Get(filter) Get single entity by filter
GetAll() Get all entities (with optional filter, sort, pagination)
Count(filter) Count matching entities
Add(entity) Insert a single entity
AddRange(entities) Insert multiple entities
Update(entity) Replace a single entity
Update(entities) Bulk replace multiple entities
Delete(id) Delete by ID
Delete(ids) Delete multiple by IDs
Delete(filter) Delete by filter

All methods support CancellationToken and accept native MongoDB driver types (FilterDefinition<T>, SortDefinition<T>, etc.).

Health Checks

builder.Services.AddHealthChecks()
    .AddMongoRepository(options =>
    {
        options.SingleFailureIsUnhealthy = true;
        options.MissingConnectionIsFailure = false;
    });

app.MapHealthChecks("/health");
Scenario Default SingleFailureIsUnhealthy
Both connections OK Healthy Healthy
One connection fails Degraded Unhealthy
Both connections fail Unhealthy Unhealthy

Documentation

Full documentation with API reference: emuuu.github.io/MongoRepository

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0 97 5/27/2026
10.1.0 234 1/16/2026
10.0.0 135 1/16/2026
9.0.4 301 9/10/2025

Multi-document transaction support — breaking signature change.

   Added:
   - StartSessionAsync, SupportsTransactionsAsync, ExecuteInTransactionAsync on IReadWriteRepository.
   - Optional IClientSessionHandle parameter on all non-obsolete read and mutation methods.
   - When a session is supplied to a read method, the read targets the read/write collection (sessions are client-bound; cross-client reads would throw).

   Breaking:
   - The optional IClientSessionHandle parameter is inserted before CancellationToken. Positional callers like repo.Get(id, ct) must switch to repo.Get(id, cancellationToken: ct). External implementers of IReadOnlyDataRepository / IReadWriteRepository must add the new parameters.

   See CHANGELOG.md for the full migration notes.