The core library of the .NET wrapper for MongoDB built for .NET Standard 2.0.
$ dotnet add package EphemeralMongo.CoreEphemeralMongo is a set of three NuGet packages wrapping the binaries of MongoDB 4, 5 and 6. Each package targets .NET Standard 2.0, which means you can use it with .NET Framework 4.5.2 up to .NET 6 and later.
The supported operating systems are Linux, macOS and Windows on their x64 architecture versions only. Each package provides access to:
This project is very much inspired from Mongo2Go but contains several improvements:
ManualResetEventSlim.| Package | Description | Link |
|---|---|---|
| EphemeralMongo4 | All-in-one package for MongoDB 4.4.15 on Linux, macOS and Windows | |
| EphemeralMongo5 | All-in-one package for MongoDB 5.0.10 on Linux, macOS and Windows |
| EphemeralMongo6 | All-in-one package for MongoDB 6.0.0 on Linux, macOS and Windows |
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
StandardOuputLogger = 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
};
// 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),Try not to call MongoRunner.Run in parallel, 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.