BrandUp.MongoDB.Testing.Mongo2Go
10.0.12
dotnet add package BrandUp.MongoDB.Testing.Mongo2Go --version 10.0.12
NuGet\Install-Package BrandUp.MongoDB.Testing.Mongo2Go -Version 10.0.12
<PackageReference Include="BrandUp.MongoDB.Testing.Mongo2Go" Version="10.0.12" />
<PackageVersion Include="BrandUp.MongoDB.Testing.Mongo2Go" Version="10.0.12" />
<PackageReference Include="BrandUp.MongoDB.Testing.Mongo2Go" />
paket add BrandUp.MongoDB.Testing.Mongo2Go --version 10.0.12
#r "nuget: BrandUp.MongoDB.Testing.Mongo2Go, 10.0.12"
#:package BrandUp.MongoDB.Testing.Mongo2Go@10.0.12
#addin nuget:?package=BrandUp.MongoDB.Testing.Mongo2Go&version=10.0.12
#tool nuget:?package=BrandUp.MongoDB.Testing.Mongo2Go&version=10.0.12
BrandUp.MongoDB
A thin, DI-friendly layer on top of the official MongoDB.Driver that gives you EF-style
database contexts, automatic collection registration, conventions, transactions, and
ergonomic test helpers.
Installation
NuGet: BrandUp.MongoDB
dotnet add package BrandUp.MongoDB
Define a context
Declare a class deriving from MongoDbContext. Each IMongoCollection<TDocument>
property is auto-registered as a collection. Mark every document with
[MongoCollection].
using BrandUp.MongoDB;
using MongoDB.Driver;
public class WebSiteDbContext : MongoDbContext, ICommentsDbContext
{
public IMongoCollection<ArticleDocument> Articles => GetCollection<ArticleDocument>();
public IMongoCollection<CommentDocument> Comments => GetCollection<CommentDocument>();
}
public interface ICommentsDbContext
{
IMongoCollection<CommentDocument> Comments { get; }
}
[MongoCollection(CollectionName = "Articles")]
public class ArticleDocument { /* ... */ }
[MongoCollection(CollectionName = "Comments")]
public class CommentDocument { /* ... */ }
Register with DI
services.AddMongoDb(options =>
{
options.ConnectionString = "mongodb://localhost:27017";
});
services
.AddMongoDbContext<WebSiteDbContext>(options =>
{
options.DatabaseName = "WebSite";
})
.AddExtension<WebSiteDbContext, ICommentsDbContext>()
.UseCamelCaseElementName()
.UseIgnoreIfNull(true)
.UseIgnoreIfDefault(false);
Resolve and use
var dbContext = serviceProvider.GetRequiredService<WebSiteDbContext>();
var commentsDbContext = serviceProvider.GetRequiredService<ICommentsDbContext>();
await dbContext.Articles.InsertOneAsync(new ArticleDocument { /* ... */ });
Transactions (await using)
MongoDbSession is registered per DI scope. ITransactionFactory and
IClientSessionHandle are exposed alongside it. The transaction handle implements
both IDisposable and IAsyncDisposable, so prefer await using — that flows
the rollback path through AbortTransactionAsync instead of blocking the thread.
using var scope = serviceProvider.CreateAsyncScope();
var dbContext = scope.ServiceProvider.GetRequiredService<WebSiteDbContext>();
var transactionFactory = scope.ServiceProvider.GetRequiredService<ITransactionFactory>();
var session = scope.ServiceProvider.GetRequiredService<IClientSessionHandle>();
await using var transaction = await transactionFactory.BeginAsync(ct);
await dbContext.Articles.InsertOneAsync(session, new ArticleDocument { /* ... */ }, cancellationToken: ct);
await transaction.CommitAsync(ct);
// If CommitAsync is not reached (exception, early return), DisposeAsync aborts the transaction.
Per-collection configuration
Tweak MongoCollectionSettings or CreateCollectionOptions for a specific document type
without subclassing the metadata:
services
.AddMongoDbContext<WebSiteDbContext>(options => options.DatabaseName = "WebSite")
.ConfigureCollection<ArticleDocument>(
configureSettings: s => s.ReadPreference = ReadPreference.SecondaryPreferred,
configureCreate: c => c.Capped = false);
The configureCreate hook only fires the first time the context boots against a fresh
database — when the collection does not yet exist and is about to be created.
Testing
In-memory fakes — BrandUp.MongoDB.Testing
NuGet: BrandUp.MongoDB.Testing
services.AddFakeMongoDb();
Fast and dependency-free; no MongoDB process required. Suitable when you only need the in-memory shape of the driver API — many advanced operators (aggregation, change streams, full filter pipelines) are intentionally not implemented.
Real mongod — BrandUp.MongoDB.Testing.EphemeralMongo
NuGet: BrandUp.MongoDB.Testing.EphemeralMongo
services.AddEphemeralMongoDb();
Spins up a real ephemeral mongod (single-node replica set, so transactions work)
via EphemeralMongo. Pick this for
integration tests where you want the actual driver behaviour.
Legacy — BrandUp.MongoDB.Testing.Mongo2Go (deprecated)
The Mongo2Go-backed helper is still published for backwards compatibility but is no
longer maintained upstream. Both AddTestMongoDb() and Mongo2GoDbClientFactory
are marked [Obsolete]; please migrate to AddEphemeralMongoDb().
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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
- BrandUp.MongoDB (>= 10.0.12)
- Mongo2Go (>= 4.1.0)
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 |
|---|---|---|
| 10.0.12 | 42 | 5/28/2026 |
| 10.0.9 | 94 | 5/20/2026 |
| 10.0.8 | 97 | 5/12/2026 |
| 10.0.7 | 98 | 5/12/2026 |
| 10.0.6 | 99 | 5/5/2026 |
| 10.0.5 | 114 | 4/27/2026 |
| 10.0.4 | 124 | 4/20/2026 |
| 10.0.3 | 179 | 3/16/2026 |
| 10.0.2 | 121 | 3/11/2026 |
| 10.0.1 | 178 | 1/3/2026 |
| 8.0.9 | 329 | 7/14/2025 |
| 8.0.8 | 505 | 6/11/2025 |
| 8.0.3 | 323 | 3/29/2025 |
| 8.0.2 | 371 | 3/9/2025 |
| 8.0.1 | 288 | 12/22/2024 |
| 7.3.2 | 363 | 7/9/2024 |
| 7.3.1 | 290 | 7/9/2024 |
| 7.2.5 | 323 | 6/30/2024 |
| 7.2.3 | 350 | 4/29/2024 |
| 7.1.5 | 300 | 4/29/2024 |