Skip to main content

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 Methods

MethodWhat It RegistersKey Options
AddMongoDbSnapshotStore(opts)ISnapshotStoreCollectionName
AddMongoDbProjectionStore<T>(connStr, dbName, opts?)IProjectionStore<T>CollectionName
AddMongoDbOutboxStore(opts)IOutboxStoreCollectionName
AddMongoDbSagaStore(opts)ISagaStoreCollectionName

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");
});

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