.NET native wrapper for MongoDB 6.0.21 built for .NET Standard 2.0.
$ dotnet add package EphemeralMongo6EphemeralMongo 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:
This project follows in the footsteps of Mongo2Go and expands upon its foundation.
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");
}mongod, mongoimport, and mongoexport) are copied to your project output directory,MongoRunner.Run always starts a new mongod process with a random available port,UseSingleNodeReplicaSet and AdditionalArguments),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'))" />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).
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.
MongoRunnerOptions.StandardOutputLogger.