MongoDB Setup
Minimal example to enable MongoDB persistence for Elsa Workflows, including connection configuration and indexing notes.
This document provides a minimal, copy-pasteable example for configuring Elsa Workflows with MongoDB persistence.
Prerequisites
.NET 8.0 or later
MongoDB 4.4 or later
Elsa v3.x packages
NuGet Packages
dotnet add package Elsa
dotnet add package Elsa.Persistence.MongoDbMinimal Configuration
Program.cs
using Elsa.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Get connection string from configuration
var mongoConnectionString = builder.Configuration.GetConnectionString("MongoDb")
?? throw new InvalidOperationException("Connection string 'MongoDb' not found.");
builder.Services.AddElsa(elsa =>
{
// Configure the shared MongoDB connection.
elsa.UseMongoDb(mongoConnectionString);
// Configure workflow management (definitions, instances)
elsa.UseWorkflowManagement(management =>
{
management.UseMongoDb();
});
// Configure workflow runtime (bookmarks, inbox, execution logs)
elsa.UseWorkflowRuntime(runtime =>
{
runtime.UseMongoDb();
});
// Enable HTTP activities (optional)
elsa.UseHttp();
// Enable scheduling activities (optional)
elsa.UseScheduling();
// Enable API endpoints
elsa.UseWorkflowsApi();
});
var app = builder.Build();
// Map Elsa API endpoints
app.UseWorkflows();
app.Run();appsettings.json
With Authentication:
Replica Set:
Index Creation
Elsa creates the MongoDB indexes it needs on startup. You do not need to run a separate index creation script for the built-in workflow management and runtime stores.
Elsa uses snake_case collection names, including:
workflow_definitionsworkflow_instancestriggersbookmarksbookmark_queue_itemsworkflow_execution_logsactivity_execution_logskey_value_pairs
Use MongoDB shell commands only for verification or for additional workload-specific indexes:
Interrupted-workflow recovery
The 3.8.1 MongoDB workflow-instance store supports the conditional TryMarkInterruptedAsync update used by force-drain recovery. Use durable MongoDB collections for workflow instances, bookmarks, and execution logs when instances must survive a process restart. See Persistence provider recovery support for the deployment checklist.
Advanced Configuration
Custom Database and Collection Names
The database name is read from the connection string. For custom collection names, replace the CollectionNamingStrategy on the MongoDB feature.
Connection Pool Settings
Configure MongoDB driver settings such as pool sizes in the connection string:
Read Preference for Replicas
For read-heavy workloads with replica sets:
Mapping Considerations
Custom Activity Data
When storing custom data in activities, ensure it's BSON-serializable:
BSON Serialization Settings
Elsa uses MongoDB driver conventions. For custom serialization needs:
TTL Collections
MongoDB supports TTL (Time-To-Live) indexes for automatic document expiration:
Troubleshooting
Connection Issues
Error: Unable to connect to server
Solutions:
Verify MongoDB is running:
mongosh --eval "db.runCommand({ping: 1})"Check connection string format
Verify network connectivity and firewall rules
For replica sets, ensure all nodes are accessible
Error: Authentication failed
Solutions:
Verify username/password
Check
authSourceparameter in connection stringEnsure user has appropriate roles:
readWriteon the elsa database
Performance Issues
Slow queries:
Verify indexes are created:
db.collection.getIndexes()Use explain plans:
db.collection.find({...}).explain("executionStats")Check for collection scans in slow query logs
High memory usage:
Review connection pool settings
Consider adding a TTL index for log collections
Implement retention policies for old workflow instances
Logging
Enable MongoDB driver logging:
Related Documentation
Persistence Guide — Overview and provider comparison
Indexing Notes — Detailed indexing guidance
MongoDB Documentation — Official MongoDB docs
Last Updated: 2025-11-28
Last updated