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
<PackageReference Include="Regira.DAL.MongoDB" Version="6.3.2" />
<PackageVersion Include="Regira.DAL.MongoDB" Version="6.3.2" />
<PackageReference Include="Regira.DAL.MongoDB" />
paket add Regira.DAL.MongoDB --version 6.3.2
#r "nuget: Regira.DAL.MongoDB, 6.3.2"
#:package Regira.DAL.MongoDB@6.3.2
#addin nuget:?package=Regira.DAL.MongoDB&version=6.3.2
#tool nuget:?package=Regira.DAL.MongoDB&version=6.3.2
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
- Index — Settings, communicator, repository, and backup/restore
- 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 | 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 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. |
-
net10.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.12)
- MongoDB.Driver (>= 3.11.2)
- Regira.Common (>= 6.3.2)
- Regira.System (>= 6.3.2)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.12)
- MongoDB.Driver (>= 3.11.2)
- Regira.Common (>= 6.3.2)
- Regira.System (>= 6.3.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.