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
                    
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="Pinqponq.Database.Mongo" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pinqponq.Database.Mongo" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Pinqponq.Database.Mongo" />
                    
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 Pinqponq.Database.Mongo --version 1.1.0
                    
#r "nuget: Pinqponq.Database.Mongo, 1.1.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 Pinqponq.Database.Mongo@1.1.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=Pinqponq.Database.Mongo&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Pinqponq.Database.Mongo&version=1.1.0
                    
Install as a Cake Tool

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, or net10.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. AddPinqponqMongo registers IMongoClient as a singleton built from ConnectionString, and IMongoDatabase as a second singleton resolved via client.GetDatabase(DatabaseName) — both use TryAddSingleton, so an app-provided registration for either interface takes precedence if added first.
  • Health check is a real ping. MongoHealthCheck issues a single { ping: 1 } command against the resolved database via RunCommandAsync<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.Postgres and Pinqponq.Database.Mssql.
  • RetryCount was removed. An earlier revision of this package exposed a RetryCount option on MongoOptions; 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 the RetryCount assignment — 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.

Samples

Try this package in the browser via Pinqponq.Playground — see samples/README.md.

Repository

https://github.com/pinqponq/pinqnuqets

Product 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. 
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
1.1.0 95 10/17/2026
1.0.1 587 8/7/2026
1.0.0 122 8/7/2026