githubEdit

MongoDB Setup

Minimal example to enable MongoDB persistence for Elsa Workflows, including connection configuration and index creation guidance.

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.MongoDb
dotnet add package MongoDB.Driver

Minimal 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 workflow management (definitions, instances)
    elsa.UseWorkflowManagement(management =>
    {
        management.UseMongoDb(mongo =>
        {
            mongo.ConnectionString = mongoConnectionString;
            // Optional: Specify database name (defaults to 'elsa')
            // mongo.DatabaseName = "elsa_workflows";
        });
    });
    
    // Configure workflow runtime (bookmarks, inbox, execution logs)
    elsa.UseWorkflowRuntime(runtime =>
    {
        runtime.UseMongoDb(mongo =>
        {
            mongo.ConnectionString = mongoConnectionString;
        });
    });
    
    // 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

MongoDB does not automatically create indexes. Create these indexes for production performance:

Using MongoDB Shell

Save as Script

Save the above as create-indexes.js and run:

Advanced Configuration

Custom Database and Collection Names

Connection Pool Settings

Configure MongoDB driver settings for high-concurrency scenarios:

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 authSource parameter in connection string

  • Ensure user has appropriate roles: readWrite on the elsa database

Performance Issues

Slow queries:

  1. Verify indexes are created: db.collection.getIndexes()

  2. Use explain plans: db.collection.find({...}).explain("executionStats")

  3. 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:


Last Updated: 2025-11-28

Last updated