MongoGenericRepository.HealthChecks
11.0.0
dotnet add package MongoGenericRepository.HealthChecks --version 11.0.0
NuGet\Install-Package MongoGenericRepository.HealthChecks -Version 11.0.0
<PackageReference Include="MongoGenericRepository.HealthChecks" Version="11.0.0" />
<PackageVersion Include="MongoGenericRepository.HealthChecks" Version="11.0.0" />
<PackageReference Include="MongoGenericRepository.HealthChecks" />
paket add MongoGenericRepository.HealthChecks --version 11.0.0
#r "nuget: MongoGenericRepository.HealthChecks, 11.0.0"
#:package MongoGenericRepository.HealthChecks@11.0.0
#addin nuget:?package=MongoGenericRepository.HealthChecks&version=11.0.0
#tool nuget:?package=MongoGenericRepository.HealthChecks&version=11.0.0
<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.
| 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 | Versions 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. |
-
.NETStandard 2.1
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.11)
- MongoGenericRepository (>= 11.0.0)
-
net10.0
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.8)
- MongoGenericRepository (>= 11.0.0)
-
net8.0
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.11)
- MongoGenericRepository (>= 11.0.0)
-
net9.0
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 9.0.1)
- MongoGenericRepository (>= 11.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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.