For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.MongoDb

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 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_definitions

  • workflow_instances

  • triggers

  • bookmarks

  • bookmark_queue_items

  • workflow_execution_logs

  • activity_execution_logs

  • key_value_pairs

Use MongoDB shell commands only for verification or for additional workload-specific indexes:

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 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