![]() |
VOOZH | about |
dotnet add package Dosaic.Plugins.Persistence.MongoDb --version 1.2.34
NuGet\Install-Package Dosaic.Plugins.Persistence.MongoDb -Version 1.2.34
<PackageReference Include="Dosaic.Plugins.Persistence.MongoDb" Version="1.2.34" />
<PackageVersion Include="Dosaic.Plugins.Persistence.MongoDb" Version="1.2.34" />Directory.Packages.props
<PackageReference Include="Dosaic.Plugins.Persistence.MongoDb" />Project file
paket add Dosaic.Plugins.Persistence.MongoDb --version 1.2.34
#r "nuget: Dosaic.Plugins.Persistence.MongoDb, 1.2.34"
#:package Dosaic.Plugins.Persistence.MongoDb@1.2.34
#addin nuget:?package=Dosaic.Plugins.Persistence.MongoDb&version=1.2.34Install as a Cake Addin
#tool nuget:?package=Dosaic.Plugins.Persistence.MongoDb&version=1.2.34Install as a Cake Tool
Dosaic.Plugins.Persistence.MongoDb is a Dosaic plugin that integrates MongoDB into your application. It registers a singleton IMongoDbInstance for collection access, wires up OpenTelemetry distributed tracing, exposes driver-level metrics counters, and adds a readiness health check — all driven by a single configuration section.
dotnet add package Dosaic.Plugins.Persistence.MongoDb
Or as a package reference in your .csproj:
<PackageReference Include="Dosaic.Plugins.Persistence.MongoDb" Version="" />
mongodb:
host: "localhost"
port: 27017
database: "mydb"
authDatabase: "" # optional — falls back to 'database' when empty
user: "mongouser"
password: "s3cr3t"
{
"mongodb": {
"host": "localhost",
"port": 27017,
"database": "mydb",
"authDatabase": "",
"user": "mongouser",
"password": "s3cr3t"
}
}
The [Configuration("mongodb")] attribute causes Dosaic's TypeImplementationResolver to automatically bind this section and inject the result as a constructor dependency.
[Configuration("mongodb")]
public class MongoDbConfiguration
{
public string Host { get; set; } = null!;
public int Port { get; set; }
public string Database { get; set; } = null!;
/// <summary>
/// The database used for authentication. Falls back to Database when empty.
/// </summary>
public string AuthDatabase { get; set; } = null!;
public string User { get; set; } = null!;
public string Password { get; set; } = null!;
}
Authentication is optional. When
UserandPasswordare both empty the driver connects without credentials.
A docker-compose.yml is included at the root of the plugin directory for quick local setup.
Inject IMongoDbInstance and call GetCollectionFor<T>(). The collection name is derived from the type name (typeof(T).Name).
public class OrderRepository
{
private readonly IMongoCollection<Order> _orders;
public OrderRepository(IMongoDbInstance mongoDb)
{
_orders = mongoDb.GetCollectionFor<Order>();
}
public async Task<Order?> FindByIdAsync(Guid id, CancellationToken ct = default)
{
var filter = Builders<Order>.Filter.Eq(o => o.Id, id);
return await _orders.Find(filter).FirstOrDefaultAsync(ct);
}
public async Task InsertAsync(Order order, CancellationToken ct = default)
{
await _orders.InsertOneAsync(order, cancellationToken: ct);
}
public async Task<List<Order>> GetAllAsync(CancellationToken ct = default)
{
return await _orders.Find(_ => true).ToListAsync(ct);
}
}
When you need lower-level access (e.g. to run admin commands), IMongoDbInstance exposes the underlying MongoClient:
public class DatabaseAdminService
{
private readonly MongoClient _client;
public DatabaseAdminService(IMongoDbInstance mongoDb)
{
_client = mongoDb.Client;
}
public async Task<List<string>> ListDatabaseNamesAsync(CancellationToken ct = default)
{
var cursor = await _client.ListDatabaseNamesAsync(ct);
return await cursor.ToListAsync(ct);
}
}
public interface IMongoDbInstance
{
/// <summary>The configured MongoClient singleton.</summary>
MongoClient Client { get; }
/// <summary>
/// Returns the typed collection whose name equals typeof(T).Name.
/// </summary>
IMongoCollection<T> GetCollectionFor<T>();
}
MongoDbPlugin implements both IPluginServiceConfiguration and IPluginHealthChecksConfiguration. It is automatically discovered by the Dosaic source generator and requires no manual registration.
The following services are registered in the DI container:
| Service | Lifetime | Description |
|---|---|---|
MongoDbConfiguration |
Singleton | Bound configuration object |
IMongoDbInstance (MongoDbInstance) |
Singleton | MongoDB connection and collection factory |
A readiness health check named "mongo" is registered under the readiness tag. It is available at the standard Dosaic health endpoint:
GET /health/readiness
The check connects to the configured host/port and verifies the named database is reachable. Authentication credentials are forwarded when present.
The plugin subscribes the MongoDB.Driver.Core.Extensions.DiagnosticSources activity source so every MongoDB command appears as a span in your distributed traces:
ActivitySource: MongoDB.Driver.Core.Extensions.DiagnosticSources
No additional configuration is required — tracing is enabled automatically when the plugin is active.
MongoDbInstance subscribes to MongoDB driver events and emits OpenTelemetry counter metrics in snake_case format. The following counters are tracked:
| Metric name | Driver event |
|---|---|
mongodb_driver_connection_opened_event_total |
Connection opened |
mongodb_driver_connection_closed_event_total |
Connection closed |
mongodb_driver_connection_failed_event_total |
Connection failed |
mongodb_driver_connection_opening_failed_event_total |
Connection opening failed |
mongodb_driver_server_heartbeat_started_event_total |
Heartbeat started |
mongodb_driver_server_heartbeat_succeeded_event_total |
Heartbeat succeeded |
mongodb_driver_server_heartbeat_failed_event_total |
Heartbeat failed |
mongodb_driver_command_started_event_total |
Command started |
mongodb_driver_command_succeeded_event_total |
Command succeeded |
mongodb_driver_command_failed_event_total |
Command failed |
mongodb_driver_cluster_selecting_server_failed_event_total |
Server selection failed |
mongodb_driver_connection_receiving_message_failed_event_total |
Receive message failed |
mongodb_driver_connection_sending_messages_failed_event_total |
Send messages failed |
These counters are exposed through Dosaic's default Prometheus endpoint (/metrics).
| Setting | Value |
|---|---|
| Connect timeout | 5 seconds |
| Heartbeat interval | 5 seconds |
| Authentication | Optional — skipped when User or Password is empty |
| Auth database | Uses AuthDatabase when set, otherwise falls back to Database |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.34 | 94 | 6/10/2026 |
| 1.2.33 | 102 | 6/2/2026 |
| 1.2.31 | 102 | 5/28/2026 |
| 1.2.30 | 107 | 5/7/2026 |
| 1.2.29 | 102 | 5/5/2026 |
| 1.2.28 | 100 | 4/30/2026 |
| 1.2.27 | 106 | 4/29/2026 |
| 1.2.26 | 103 | 4/29/2026 |
| 1.2.25 | 102 | 4/27/2026 |
| 1.2.24 | 100 | 4/21/2026 |
| 1.2.23 | 106 | 4/14/2026 |
| 1.2.22 | 109 | 4/10/2026 |
| 1.2.21 | 113 | 4/10/2026 |
| 1.2.20 | 114 | 4/10/2026 |
| 1.2.19 | 105 | 4/9/2026 |
| 1.2.18 | 114 | 4/2/2026 |
| 1.2.17 | 104 | 4/1/2026 |
| 1.2.16 | 107 | 4/1/2026 |
| 1.2.15 | 109 | 3/31/2026 |
| 1.2.14 | 114 | 3/30/2026 |