Regira.DAL.MongoDB 6.3.2

dotnet add package Regira.DAL.MongoDB --version 6.3.2
                    
NuGet\Install-Package Regira.DAL.MongoDB -Version 6.3.2
                    
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="Regira.DAL.MongoDB" Version="6.3.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Regira.DAL.MongoDB" Version="6.3.2" />
                    
Directory.Packages.props
<PackageReference Include="Regira.DAL.MongoDB" />
                    
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 Regira.DAL.MongoDB --version 6.3.2
                    
#r "nuget: Regira.DAL.MongoDB, 6.3.2"
                    
#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 Regira.DAL.MongoDB@6.3.2
                    
#: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=Regira.DAL.MongoDB&version=6.3.2
                    
Install as a Cake Addin
#tool nuget:?package=Regira.DAL.MongoDB&version=6.3.2
                    
Install as a Cake Tool

Regira DAL — MongoDB

Regira DAL.MongoDB provides lightweight MongoDB connectivity using the MongoDB Driver, plus backup/restore via mongodump/mongorestore.

Projects

Project Package Backend CRUD Backup / Restore
DAL.MongoDB Regira.DAL.MongoDB MongoDB via MongoDB Driver ✓ (mongodump)

Installation

<PackageReference Include="Regira.DAL.MongoDB" Version="6.*" />

MongoSettings

Property Type Description
Host string Hostname / replica set
DatabaseName string Target database
Port string Port (default 27017)
Username string? Auth username
Password string? Auth password
AuthenticationDatabase string? authSource — the database holding the credentials, when that is not DatabaseName. Left empty, MongoDB resolves it itself: DatabaseName, or admin when no database is named
UseSecure (UseTls) bool TLS/SSL
UseSrv bool The mongodb+srv:// scheme, where DNS supplies the hosts and the port
UriOptions IList<KeyValuePair<string, string>> Every other connection-string option — authMechanism, replicaSet, directConnection, readPreference, tlsCAFile, … — in order and unescaped, a repeated one once per occurrence; an explicit tls=false is kept too, and left out while UseSecure is on
var settings = new MongoSettings("localhost", "mydb");
// or parse from connection string:
settings = MongoSettings.FromConnectionString("mongodb://user:pass@host:27017/mydb?authSource=admin");

A Username makes every connection an authenticated one — the communicator's and mongodump/mongorestore's alike. Options kept in UriOptions reach both as well, so an X.509 login or a replica set reached through one member connects the way the connection string says.

BuildConnectionString() composes the URI back, percent-encoding the credentials and every option; BuildConnectionString(includePassword: false) composes the same URI without the password, for a caller that passes the password through a channel of its own.

MongoCommunicator

var settings = new MongoSettings("localhost", "mydb");
var comm = new MongoCommunicator(settings);
var names = comm.ListCollectionNames();   // IAsyncEnumerable<string>

The underlying IMongoDatabase (Database) is protected internal — it is not accessible on the communicator from consumer code, only from repository classes derived from MongoDbRepositoryBase<TEntity>.

MongoDbRepositoryBase<TEntity>

Extend this to build a repository (TEntity must be a class with a parameterless constructor). The base constructor takes the communicator, an ISerializer, id accessor delegates, and an optional collection name. Override GetFilter(), SortResult(), and PageResult() for custom queries.

public class Product
{
    public string? Id { get; set; }
    public string? Name { get; set; }
}

public class ProductRepository(MongoCommunicator comm, ISerializer serializer)
    : MongoDbRepositoryBase<Product>(
        comm,
        serializer,
        getIdFunc: p => p.Id,
        setIdAction: (p, id) => p.Id = id,
        collectionName: "products")
{
    protected override FilterDefinition<BsonDocument> GetFilter(IDictionary<string, object?>? so)
    {
        var filter = base.GetFilter(so);
        if (so?.TryGetValue("name", out var name) == true && name != null)
        {
            filter &= Builders<BsonDocument>.Filter.Eq("Name", name.ToString());
        }
        return filter;
    }
}

CRUD methods:

Task<TEntity?>             Details(object id)
Task<IEnumerable<TEntity>> List(object? searchObject = null)
Task<long>                 Count(object? searchObject = null)
Task<long>                 Save(TEntity item)     // inserted (1) or modified count
Task<long>                 Delete(TEntity item)   // deleted count

MongoBackupService / MongoRestoreService

Requires the mongodump / mongorestore executables of the MongoDB Database Tools 100.3.0 or later in MongoOptions.ToolsDirectory. Both services also take an IProcessHelper (e.g. ProcessHelper from Regira.System) to run those executables, and an optional ILogger that records the command without its password.

var settings = new MongoSettings("mongo.example.com", "mydb", username: "app", password: "s3cret")
{
    AuthenticationDatabase = "admin"
};
var options = new MongoOptions
{
    DbSettings     = settings,
    ToolsDirectory = "/usr/bin"
};
IProcessHelper processHelper = new ProcessHelper();

IMemoryFile backup = await new MongoBackupService(options, processHelper).Backup();
await new MongoRestoreService(options, processHelper).Restore(backup);

Authentication

The connection reaches the tool in a temporary YAML file it reads through --config, deleted again once the tool has run: the URI — username, authSource for AuthenticationDatabase, tls=true for UseSecure, and every UriOptions entry — and the password beside it. The tools take both on the command line as well, but a command line is readable by every other process on the machine for as long as the dump runs, and a URI can carry secrets of its own (tlsCertificateKeyFilePassword, an AWS_SESSION_TOKEN in authMechanismProperties). They accept the password nowhere else, reading no environment variable and answering their interactive prompt from the console rather than from stdin. The debug log shows the URI without the password and with those options masked.

A Password without a Username is refused with an ArgumentException: there is nothing to authenticate as. So is a Username without a Password, which would leave the tool waiting at its prompt — unless UriOptions names an authMechanism that needs no password (MONGODB-X509, MONGODB-AWS, GSSAPI, MONGODB-OIDC).

Both services start the executable directly, without a shell in between, so an IProcessHelper of your own sees ExecuteFile rather than ExecuteCommand.

Backup/Restore contracts

Both services implement the shared contracts from Common:

public interface IDbBackupService  { Task<IMemoryFile> Backup(); }
public interface IDbRestoreService { Task Restore(IMemoryFile file); }

Overview

  1. Index — Settings, communicator, repository, and backup/restore
  2. Examples — Connect, query, and backup

License

Apache License 2.0 — this package contains no license validation and no runtime limits. See LICENSE. A few companion packages are commercially licensed with a free tier; see the licensing overview.

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 was computed.  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
6.3.2 40 9/19/2026
6.3.1 46 9/17/2026
6.2.1 107 9/5/2026
6.1.3 135 8/26/2026
6.1.2 131 8/16/2026
6.1.1 110 8/12/2026
6.1.0 117 8/10/2026