EphemeralMongo7 2.0.0

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

EphemeralMongo - temporary and disposable MongoDB for integration tests and local debugging

ci publish

Introduction

EphemeralMongo is a set of multiple NuGet packages wrapping the binaries of MongoDB 6, 7, and 8. Each package targets .NET Standard 2.0, which means you can use it with .NET Framework 4.5.2 up to .NET 9 and later.

The supported operating systems are Linux (x64), macOS (arm64), and Windows (x64). Each package provides access to:

  • Multiple ephemeral and isolated MongoDB databases for running tests,
  • A quick way to set up a MongoDB database for a local development environment,
  • mongoexport and mongoimport tools for exporting and importing collections,
  • Support for single-node replica sets, enabling transactions and change streams.

This project follows in the footsteps of Mongo2Go and expands upon its foundation.

Package Description Link
EphemeralMongo6 All-in-one package for MongoDB 6.0.21 on Linux, macOS, and Windows nuget
EphemeralMongo7 All-in-one package for MongoDB 7.0.18 on Linux, macOS, and Windows nuget
EphemeralMongo8 All-in-one package for MongoDB 8.0.6 on Linux, macOS, and Windows nuget

Usage

Use the static MongoRunner.Run() method to create a disposable instance that provides access to a MongoDB connection string, import, and export tools:

// All properties below are optional. The whole "options" instance is optional too!
var options = new MongoRunnerOptions
{
    UseSingleNodeReplicaSet = true, // Default: false
    StandardOutputLogger = line => Console.WriteLine(line), // Default: null
    StandardErrorLogger = line => Console.WriteLine(line), // Default: null
    DataDirectory = "/path/to/data/", // Default: null
    BinaryDirectory = "/path/to/mongo/bin/", // Default: null
    ConnectionTimeout = TimeSpan.FromSeconds(10), // Default: 30 seconds
    ReplicaSetSetupTimeout = TimeSpan.FromSeconds(5), // Default: 10 seconds
    AdditionalArguments = "--quiet", // Default: null
    MongoPort = 27017, // Default: random available port
    DataDirectoryLifetime = TimeSpan.FromDays(1), // Default: 12 hours
};

// Disposing the runner will kill the MongoDB process (mongod) and delete the associated data directory
using (var runner = MongoRunner.Run(options))
{
    var database = new MongoClient(runner.ConnectionString).GetDatabase("default");

    // Do something with the database
    database.CreateCollection("people");

    // Export a collection. Full method signature:
    // Export(string database, string collection, string outputFilePath, string? additionalArguments = null)
    runner.Export("default", "people", "/path/to/default.json");

    // Import a collection. Full method signature:
    // Import(string database, string collection, string inputFilePath, string? additionalArguments = null, bool drop = false)
    runner.Import("default", "people", "/path/to/default.json");
}

How it works

  • At build time, the MongoDB binaries (mongod, mongoimport, and mongoexport) are copied to your project output directory,
  • At runtime, the library chooses the right binaries for your operating system,
  • MongoRunner.Run always starts a new mongod process with a random available port,
  • The resulting connection string will depend on your options (UseSingleNodeReplicaSet and AdditionalArguments),
  • By default, a unique temporary data directory is used.

Reducing the download size

EphemeralMongo6, 7, and 8 are NuGet metapackages that reference dedicated runtime packages for Linux, macOS, and Windows. As of now, there isn't a way to optimize NuGet package downloads for a specific operating system (see #2). However, one can still avoid referencing the metapackage and directly reference the dependencies instead. Add MSBuild OS platform conditions and you'll get optimized NuGet imports for your OS and fewer downloads.

Instead of doing this:

<PackageReference Include="EphemeralMongo8" Version="*" />

Do this:

<PackageReference Include="EphemeralMongo.Core" Version="*" />
<PackageReference Include="EphemeralMongo8.runtime.linux-x64" Version="*" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
<PackageReference Include="EphemeralMongo8.runtime.osx-arm64" Version="*" Condition="$([MSBuild]::IsOSPlatform('OSX'))" />
<PackageReference Include="EphemeralMongo8.runtime.win-x64" Version="*" Condition="$([MSBuild]::IsOSPlatform('Windows'))" />

Windows Defender Firewall prompt

On Windows, you might get a Windows Defender Firewall prompt. This is because EphemeralMongo starts the mongod.exe process from your build output directory, and mongod.exe tries to open an available port (see here).

Optimization tips

Avoid calling MongoRunner.Run concurrently, as this will create many mongod processes and make your operating system slower. Instead, try to use a single instance and reuse it - create as many databases as you need, one per test, for example.

Check out this gist for an implementation of a reusable IMongoRunner.

Changelog

2.0.0

  • Breaking change: Support for MongoDB 5.0 and 6.0 has been removed, as their end-of-life has passed.
  • Breaking change: arm64 is now the default target for macOS. The previous target was x64.
  • Breaking change: The Linux runtime package now uses Ubuntu 22.04's MongoDB binaries instead of the 18.04 ones. OpenSSL 3.0 is now required.
  • Breaking change: Updated the MongoDB C# driver to 2.28.0, which now uses strong-named assemblies.
  • Added support for MongoDB 8.0.
  • Introduced data directory management to delete old data directories automatically.
  • Use direct connection in replica set connection strings.
  • Fixed the spelling issue in MongoRunnerOptions.StandardOutputLogger.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.0.0 26 14 hours ago
1.1.3 161,206 8/30/2023