-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
Aryeh Citron edited this page May 12, 2026
·
3 revisions
dotnet add package InMemoryEmulator.MongoDB
Requirements: .NET 8.0+, MongoDB.Driver 3.0.0+
Optional packages:
| Package | Purpose |
|---|---|
InMemoryEmulator.MongoDB.JsTriggers |
JavaScript $function, $accumulator, $where via Jint |
If your application registers IMongoClient, IMongoDatabase, or IMongoCollection<T> in your DI container, replace them in tests with a single line:
public class MyWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.UseInMemoryMongoDB(options =>
{
options.DatabaseName = "testdb";
options.AddCollection<Order>("orders");
options.AddCollection<Customer>("customers");
});
});
}
}All IMongoClient, IMongoDatabase, and IMongoCollection<T> injections will now resolve to in-memory implementations.
// Simplest: single collection
var result = InMemoryMongo.Create<Order>("orders");
IMongoCollection<Order> collection = result.Collection;
// Insert, query, update — just like real MongoDB
await collection.InsertOneAsync(new Order { Total = 99.99m });
var orders = await collection.Find(o => o.Total > 50).ToListAsync();var mongo = InMemoryMongo.Builder()
.AddDatabase("mydb", db =>
{
db.AddCollection<Order>("orders");
db.AddCollection<Customer>("customers");
})
.Build();
var ordersCollection = mongo.GetDatabase("mydb").GetCollection<Order>("orders");// Seed in test setup
var result = InMemoryMongo.Create<Product>("products");
await result.Collection.InsertManyAsync(new[]
{
new Product { Name = "Widget", Price = 9.99m },
new Product { Name = "Gadget", Price = 19.99m }
});See Seeding Data for advanced patterns including DI callbacks, file snapshots, and state persistence.
| Before (Mongo2Go) | After (InMemoryEmulator) |
|---|---|
var runner = MongoDbRunner.Start(); |
var result = InMemoryMongo.Create<T>("col"); |
var client = new MongoClient(runner.ConnectionString); |
var client = result.Client; |
runner.Dispose(); |
(garbage collected) |
| Before (Testcontainers) | After (InMemoryEmulator) |
|---|---|
var container = new MongoDbBuilder().Build(); |
var result = InMemoryMongo.Create<T>("col"); |
await container.StartAsync(); |
(instant, no startup) |
var client = new MongoClient(container.GetConnectionString()); |
var client = result.Client; |
See Migration Guide for a detailed walkthrough.
- Unit Testing — Writing tests with filter builders, update builders, and aggregation
- How It Works — Understanding the interception architecture
- Setup Guide — All 5 DI integration patterns
- Features — Complete feature reference
Getting Started
Integration & Dependency Injection
Data Management
Reference
- Feature Comparison
- Features
- Filter & Update Operators
- Aggregation Pipeline
- LINQ Support
- API Reference
Help