MongoDB Provider
The MongoDB provider implements IDocumentPersistenceProvider for flexible document storage with aggregation pipeline support, change streams, and integrated projection/snapshot/outbox stores.
Before You Start
- .NET 10.0
- A MongoDB instance (local or Atlas)
- Familiarity with data access and IDb interface
Installation
dotnet add package Excalibur.Data.MongoDB
Dependencies: Excalibur.Data.Abstractions, MongoDB.Driver
Quick Start
using Microsoft.Extensions.DependencyInjection;
// Data persistence provider (fluent builder)
services.AddExcaliburMongoDb(mongo =>
{
mongo.ConnectionString("mongodb://localhost:27017")
.DatabaseName("MyApp");
});
Builder Registration (Recommended)
All MongoDB subsystems use the fluent builder pattern with 4 canonical connection overloads:
// 1. Connection string (creates IMongoClient singleton internally)
mongo.ConnectionString("mongodb://localhost:27017");
// 2. Pre-configured IMongoClient instance
mongo.Client(existingMongoClient);
// 3. DI-aware client factory
mongo.ClientFactory(sp => sp.GetRequiredService<IMongoClient>());
// 4. Bind from appsettings.json section
mongo.BindConfiguration("MongoDB:Data");
Subsystem Entry Points
| Subsystem | Entry Point | Builder Interface |
|---|---|---|
| Data | services.AddExcaliburMongoDb(mongo => ...) | IMongoDBDataBuilder |
| Event Sourcing | es.UseMongoDB(mongo => ...) | IMongoDBEventSourcingBuilder |
| Saga | saga.UseMongoDB(mongo => ...) | IMongoDBSagaBuilder |
| Inbox | inbox.UseMongoDB(mongo => ...) | IMongoDBInboxBuilder |
| Outbox | outbox.UseMongoDB(mongo => ...) | IMongoDBOutboxBuilder |
| CDC | cdc.UseMongoDB(mongo => ...) | IMongoDbCdcBuilder |
| Leader Election | le.UseMongoDB(resourceName, mongo => ...) | IMongoDBLeaderElectionBuilder |
Legacy Registration Methods
The following standalone methods are still available for snapshots and projections:
| Method | What It Registers | Key Options |
|---|---|---|
AddMongoDbSnapshotStore(opts) | ISnapshotStore | CollectionName |
AddMongoDbProjectionStore<T>(connStr, dbName, opts?) | IProjectionStore<T> | CollectionName |
Batch Projection Registration
Register multiple projections sharing the same connection in a single call:
services.AddMongoDbProjections("mongodb://localhost:27017", "MyApp", projections =>
{
projections.Add<OrderSummary>();
projections.Add<CustomerProfile>(o => o.CollectionName = "customers");
projections.Add<InventoryView>(o => o.CollectionName = "inventory");
});
Projections are stored flat at the document root. Framework metadata is isolated under a _projection nested object to avoid collisions with your projection properties. See Projections — Document Storage Format for details.
This follows the same pattern as AddElasticSearchProjections().
Aggregation Pipelines
MongoDB's aggregation framework is accessible through the document persistence provider:
var result = await documentProvider.ExecuteAggregationAsync(aggregationRequest, cancellationToken);
Index Management
await documentProvider.ExecuteIndexOperationAsync(indexRequest, cancellationToken);
See Also
- Data Providers Overview — Architecture and core abstractions
- Cosmos DB Provider — Azure cloud-native document store
- Elasticsearch Provider — Full-text search and analytics