MVFC.Aspire.Helpers.Mongo 8.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package MVFC.Aspire.Helpers.Mongo --version 8.0.2
                    
NuGet\Install-Package MVFC.Aspire.Helpers.Mongo -Version 8.0.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="MVFC.Aspire.Helpers.Mongo" Version="8.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Aspire.Helpers.Mongo" Version="8.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Aspire.Helpers.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 MVFC.Aspire.Helpers.Mongo --version 8.0.2
                    
#r "nuget: MVFC.Aspire.Helpers.Mongo, 8.0.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 MVFC.Aspire.Helpers.Mongo@8.0.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=MVFC.Aspire.Helpers.Mongo&version=8.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Aspire.Helpers.Mongo&version=8.0.2
                    
Install as a Cake Tool

MVFC.Aspire.Helpers.Mongo

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

Helpers for integrating with MongoDB in .NET Aspire projects, including support for Replica Sets and automatic initialization.

Motivation

Setting up MongoDB locally is easy; setting up a Replica Set with realistic behavior and seed data is not.

Typical pain points in local dev:

  • Creating and initializing the Replica Set using custom scripts.
  • Remembering which ports are exposed and how to build the connection string.
  • Manually running mongorestore or ad‑hoc scripts every time you start the environment.

With .NET Aspire you can already model a MongoDB container, but you still have to wire:

  • Replica Set arguments and init scripts.
  • Data volumes for persistence between runs.
  • Connection string configuration for your projects.
  • Any logic that runs once to populate sample data.

MVFC.Aspire.Helpers.Mongo wraps these concerns into a focused API:

  • AddMongoReplicaSet(...) to get a ready‑to‑use Replica Set container.
  • Fluent methods like WithDumps(...) and WithDataVolume(...).
  • project.WithReference(mongo) to inject the connection string and trigger dumps automatically when the resource is ready.

Overview

This project facilitates the configuration and integration of MongoDB in distributed .NET Aspire applications, providing extension methods to:

  • Add a MongoDB container configured as a Replica Set.
  • Automatically initialize the Replica Set via script.
  • Populate the database with sample data using custom dumps.

Why use a Replica Set?

MongoDB only allows multi‑document transactions when configured as a Replica Set, even in local environments.

By using the helper with Replica Set, you can:

  • Simulate local transactions:
    Test transactional operations (commit/rollback) across multiple documents and collections, matching your production behavior.
  • Prepare for high availability:
    A Replica Set is the foundation for advanced MongoDB features like failover and redundancy; even locally, it helps you run closer to a real‑world setup.

Project structure

Features

  • Adds a MongoDB container configured as a Replica Set.
  • Automatic Replica Set initialization.
  • Support for populating collections with sample data.
  • Extension methods to simplify AppHost configuration.

Compatible Docker images

  • mongo

Installation

Add the NuGet package to your AppHost project:

dotnet add package MVFC.Aspire.Helpers.Mongo

Quick Aspire usage (AppHost)

Minimal example in your AppHost using Bogus to seed test data:

using Aspire.Hosting;
using Bogus;
using MVFC.Aspire.Helpers.Mongo;

var builder = DistributedApplication.CreateBuilder(args);

IReadOnlyCollection<IMongoClassDump> dumps =
[
    new MongoClassDump<TestDatabase>(
        DatabaseName: "TestDatabase",
        CollectionName: "TestCollection",
        Quantity: 100,
        Faker: new Faker<TestDatabase>()
            .CustomInstantiator(f => new TestDatabase(
                f.Person.FirstName,
                f.Person.Cpf())))
];

var mongo = builder.AddMongoReplicaSet("mongo")
    .WithDumps(dumps)
    .WithDataVolume("mongo-data");

builder.AddProject<Projects.MVFC_Aspire_Helpers_Playground_Api>("api-example")
       .WithReference(mongo)
       .WaitFor(mongo);

await builder.Build().RunAsync();

What this setup gives you:

  • A MongoDB Replica Set running in Docker.
  • A persistent Docker volume mongo-data (if configured).
  • Database/collection automatically populated with fake data at startup.
  • A connection string injected into the project configuration.

Provisioning diagram

sequenceDiagram
    participant Aspire as .NET Aspire
    participant Container as MongoDB Container
    participant Configurator as Mongo Replica Set Processor
    
    Aspire->>Container: Start container (mongo)
    Container-->>Aspire: Ready (port 27017 available)
    Aspire->>Configurator: Trigger OnResourceReady
    Configurator->>Container: Initialize Replica Set
    Configurator->>Container: Execute Dumps (if configured)
    Configurator-->>Aspire: Provisioning Completed
    Aspire->>App: Start App with ConnectionString

Fluent methods

Method Description
WithDockerImage(image, tag) Overrides the Docker image used.
WithDumps(dumps) Configures data dumps to execute on initialization.
WithDataVolume(volumeName) Enables persistence with a Docker volume.

Populating sample data

MongoClassDump<T> is a class used to facilitate the automatic insertion of sample data into MongoDB collections during environment initialization. It acts as a template to populate the database with fake documents, useful for local testing and development.

Main parameters:

  • DatabaseName: Database name.
  • CollectionName: Collection name.
  • Quantity: Number of documents.
  • Faker: Data generator (for example, using the Bogus library with the Faker class).

Configuration options

Optional parameters

  • volumeName (optional):
    Local Docker volume name used to persist data between debugging sessions.
    Default: null (volume discarded between executions).

  • connectionStringSection (optional):
    Path to the configuration section containing the MongoDB connection string.
    Default: "ConnectionStrings:mongo".

Each : indicates a level/section within the appsettings.json file:

{
  "ConnectionStrings": {
    "mongo": "mongodb://localhost:27017/"
  }
}

Visualization and ports

  • Port used: 27017 (MongoDB default).

  • View databases:
    Connect via a MongoDB client (MongoDB Compass, Robo 3T, mongosh, etc.) using:

    mongodb://localhost:27017/

Requirements

  • .NET 9+
  • Aspire.Hosting >= 9.5.0
  • Bogus >= 35.6.0
  • MongoDB.Driver >= 3.5.0

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET 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
9.0.3 88 4/12/2026
9.0.2 85 4/12/2026
9.0.1 84 4/12/2026
9.0.0 99 4/12/2026
8.0.2 89 4/11/2026
8.0.1 103 4/3/2026
8.0.0 88 4/2/2026
7.3.3 88 3/31/2026
7.3.2 94 3/30/2026
7.3.1 91 3/30/2026
7.3.0 100 3/30/2026
7.2.2 98 3/29/2026
7.2.1 92 3/29/2026
7.2.0 92 3/29/2026
7.1.0 89 3/22/2026
6.4.4 89 3/21/2026
6.4.3 90 3/15/2026
6.4.2 100 3/15/2026
6.4.1 94 3/10/2026
6.4.0 90 3/9/2026
Loading failed