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 8.0+ (or .NET 9/10 for latest features)
- 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;
services.AddMongoDbSnapshotStore(options =>
{
options.ConnectionString = "mongodb://localhost:27017";
options.DatabaseName = "MyApp";
options.CollectionName = "snapshots";
});
Registration Options
MongoDB registration uses specialized store methods — there is no single base AddMongoDB() registration. Register only the stores your application needs:
Snapshot Store
services.AddMongoDbSnapshotStore(options =>
{
options.CollectionName = "snapshots";
});
Projection Store
services.AddMongoDbProjectionStore<OrderProjection>(options =>
{
options.CollectionName = "order-projections";
});
Outbox Store
services.AddMongoDbOutboxStore(options =>
{
options.CollectionName = "outbox";
});
Saga Store
services.AddMongoDbSagaStore(options =>
{
options.CollectionName = "sagas";
});
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