-
Notifications
You must be signed in to change notification settings - Fork 0
Setup Guide
Aryeh Citron edited this page May 12, 2026
·
2 revisions
The most common pattern. Replaces all MongoDB interface registrations at once.
// Production registration (Startup.cs / Program.cs)
services.AddSingleton<IMongoClient>(new MongoClient(connectionString));
services.AddSingleton(sp => sp.GetRequiredService<IMongoClient>().GetDatabase("mydb"));
services.AddSingleton(sp => sp.GetRequiredService<IMongoDatabase>().GetCollection<Order>("orders"));
// Test override
services.UseInMemoryMongoDB(options =>
{
options.DatabaseName = "mydb";
options.AddCollection<Order>("orders");
});If your DI registers a typed client (e.g., IOrdersMongoClient : IMongoClient):
// Register specific collections for specific typed clients
services.UseInMemoryMongoDB(options =>
{
options.DatabaseName = "ordersDb";
options.AddCollection<Order>("orders");
});When repositories resolve collections via client.GetDatabase().GetCollection<T>():
services.UseInMemoryMongoDB(options =>
{
options.DatabaseName = "mydb";
options.AddCollection<Order>("orders");
options.AddCollection<Customer>("customers");
});Repositories that call client.GetDatabase("mydb").GetCollection<Order>("orders") will get the in-memory collection automatically.
When you only need to replace IMongoCollection<T> registrations (not the client or database):
services.UseInMemoryMongoCollections(options =>
{
options.DatabaseName = "mydb";
options.AddCollection<Order>("orders");
});For applications with a custom factory interface:
// Your production factory
public interface IMongoFactory
{
IMongoCollection<T> GetCollection<T>(string name);
}
// Test implementation
public class InMemoryMongoFactory : IMongoFactory
{
private readonly InMemoryMongoDatabase _db;
public InMemoryMongoFactory()
{
var client = new InMemoryMongoClient();
_db = (InMemoryMongoDatabase)client.GetDatabase("test");
}
public IMongoCollection<T> GetCollection<T>(string name)
=> _db.GetCollection<T>(name);
}
// Register in tests
services.AddSingleton<IMongoFactory>(new InMemoryMongoFactory());For the most control, instantiate directly:
var client = new InMemoryMongoClient();
var database = client.GetDatabase("mydb");
var collection = database.GetCollection<Order>("orders");Or use the builder for multi-database setups:
var mongo = InMemoryMongo.Builder()
.AddDatabase("db1", db =>
{
db.AddCollection<Order>("orders");
db.AddCollection<Customer>("customers");
})
.AddDatabase("db2", db =>
{
db.AddCollection<Product>("products");
})
.Build();| Property | Type | Description |
|---|---|---|
DatabaseName |
string |
Database name for registrations |
OnClientCreated |
Action<InMemoryMongoClient> |
Callback after client creation |
OnDatabaseCreated |
Action<InMemoryMongoDatabase> |
Callback after database creation |
AddCollection<T>(name) |
method | Register a collection |
| Property | Type | Description |
|---|---|---|
DatabaseName |
string |
Database name for registrations |
AddCollection<T>(name) |
method | Register a collection |
- Choosing Your Approach — Decision flowchart
- Seeding Data — Populating test data via callbacks
- Multi-Database — Working with multiple databases
Getting Started
Integration & Dependency Injection
Data Management
Reference
- Feature Comparison
- Features
- Filter & Update Operators
- Aggregation Pipeline
- LINQ Support
- API Reference
Help