Pinqponq.Database.Mongo
1.1.0
Prefix Reserved
dotnet add package Pinqponq.Database.Mongo --version 1.1.0
NuGet\Install-Package Pinqponq.Database.Mongo -Version 1.1.0
<PackageReference Include="Pinqponq.Database.Mongo" Version="1.1.0" />
<PackageVersion Include="Pinqponq.Database.Mongo" Version="1.1.0" />
<PackageReference Include="Pinqponq.Database.Mongo" />
paket add Pinqponq.Database.Mongo --version 1.1.0
#r "nuget: Pinqponq.Database.Mongo, 1.1.0"
#:package Pinqponq.Database.Mongo@1.1.0
#addin nuget:?package=Pinqponq.Database.Mongo&version=1.1.0
#tool nuget:?package=Pinqponq.Database.Mongo&version=1.1.0
Pinqponq.Database.Mongo
A minimal MongoDB connection layer for .NET: registers a shared IMongoClient
and the default IMongoDatabase resolved from configuration, plus a
ready-made ping health check. Like the other Pinqponq.Database.*
packages, it stops at connectivity — no repository base classes, no document
mapping conventions, no query helpers beyond paging a find into the shared
Pinqponq.Contracts paged response.
Install
dotnet add package Pinqponq.Database.Mongo
Requirements
- .NET SDK:
net8.0,net9.0, ornet10.0 - A reachable MongoDB server
MongoDB.Driver(pulled in transitively)
Quick start
using MongoDB.Driver;
using Pinqponq.Database.Mongo.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddPinqponqMongo(mongo =>
{
mongo.ConnectionString = builder.Configuration.GetConnectionString("Mongo")!;
mongo.DatabaseName = "myapp";
});
builder.Services.AddHealthChecks().AddPinqponqMongo();
var app = builder.Build();
app.MapGet("/users/{id}", async (string id, IMongoDatabase database, CancellationToken ct) =>
{
var collection = database.GetCollection<MongoDB.Bson.BsonDocument>("users");
var filter = MongoDB.Driver.Builders<MongoDB.Bson.BsonDocument>.Filter.Eq("_id", id);
var document = await collection.Find(filter).FirstOrDefaultAsync(ct);
return document is null ? Results.NotFound() : Results.Ok(document.ToJson());
});
app.Run();
Using the client or database from an injected service:
public sealed class AuditRepository(IMongoDatabase database)
{
public Task InsertAsync(BsonDocument entry, CancellationToken ct) =>
database.GetCollection<BsonDocument>("audit-log").InsertOneAsync(entry, cancellationToken: ct);
}
Paging a find
ToPagedResponseAsync counts the documents a find matches and reads only the requested page
into a PagedResponseModel<T> of Pinqponq.Contracts, with the same rules as its
ToPagedResponse (page size cap, page past the end, ArgumentException for a page number or
size under 1). Sort the find first and leave its skip and limit unset.
using Pinqponq.Database.Mongo.Pagination;
var response = await logs.Find(filter)
.SortByDescending(log => log.Date)
.ToPagedResponseAsync(paging.Page, paging.Size, page => page.Select(ToDto).ToList(), LogPaging, cancellationToken);
The package references Pinqponq.Contracts for these types, which brings the ASP.NET Core
shared framework (Microsoft.AspNetCore.App) with it.
Configuration
MongoOptions, configured through the Action<MongoOptions> delegate passed
to AddPinqponqMongo:
| Property | Type | Default | Notes |
|---|---|---|---|
ConnectionString |
string |
"" (required) |
Standard MongoDB connection string (mongodb://... or mongodb+srv://...). |
DatabaseName |
string |
"" (required) |
The database resolved into the registered IMongoDatabase. |
Options are validated at startup via ValidateOnStart() — both fields are
required; a missing DatabaseName fails fast rather than surfacing later as
a confusing "database not found" error.
Main types
| Type | Kind | Purpose |
|---|---|---|
AddPinqponqMongo(Action<MongoOptions>) |
DI extension (IServiceCollection) |
Registers a shared IMongoClient and the IMongoDatabase resolved from DatabaseName. |
AddPinqponqMongo(...) |
DI extension (IHealthChecksBuilder) |
Adds a MongoDB ping command health check (name defaults to "mongodb"). |
MongoOptions |
Options | Connection string and default database name. |
MongoHealthCheck |
Implementation | Registered by the health check extension; not usually referenced directly. |
Notes / behavior
- Two singletons, one client.
AddPinqponqMongoregistersIMongoClientas a singleton built fromConnectionString, andIMongoDatabaseas a second singleton resolved viaclient.GetDatabase(DatabaseName)— both useTryAddSingleton, so an app-provided registration for either interface takes precedence if added first. - Health check is a real
ping.MongoHealthCheckissues a single{ ping: 1 }command against the resolved database viaRunCommandAsync<BsonDocument>— it exercises the actual server round trip, not just client-side connection state. - No built-in retry policy on the connection layer itself. The MongoDB
.NET driver has its own internal retry/reconnection behavior (retryable
reads/writes, server discovery); this package does not layer an additional
Polly pipeline on top of it, unlike
Pinqponq.Database.PostgresandPinqponq.Database.Mssql. RetryCountwas removed. An earlier revision of this package exposed aRetryCountoption onMongoOptions; it has since been removed because it did not map cleanly onto the driver's own retry semantics. If you're upgrading from that version, drop theRetryCountassignment — it no longer exists.- No repositories, no entities. This package only resolves a client and a database handle. Collection access, document shape, and any ODM/mapping layer are the consuming application's responsibility.
Related packages
Samples
Try this package in the browser via Pinqponq.Playground — see samples/README.md.
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 10.0.10)
- Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Options (>= 10.0.10)
- MongoDB.Driver (>= 3.10.0)
- Pinqponq.Contracts (>= 1.0.0)
- Polly.Core (>= 8.4.2)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.11)
- Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions (>= 8.0.11)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- MongoDB.Driver (>= 3.10.0)
- Pinqponq.Contracts (>= 1.0.0)
- Polly.Core (>= 8.4.2)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.18)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 9.0.18)
- Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions (>= 9.0.18)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.18)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.18)
- Microsoft.Extensions.Options (>= 9.0.18)
- MongoDB.Driver (>= 3.10.0)
- Pinqponq.Contracts (>= 1.0.0)
- Polly.Core (>= 8.4.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.