Back to Devexpress

MongoDB Data Source in ASP.NET Core App

dashboard-403112-web-dashboard-integrate-dashboard-component-dashboard-backend-prepare-data-source-storage-for-the-aspnet-core-framework-mongodb-data-source.md

latest9.4 KB
Original Source

MongoDB Data Source in ASP.NET Core App

  • Jan 05, 2026
  • 4 minutes to read

This article shows how to add the DashboardMongoDBDataSource to an in-memory data source storage, and make it available to users. The tutorial uses a local MongoDB database and shows multiple connection options.

Prerequisites

Install the MongoDB.Driver NuGet package.

Create a MongoDB Data Source

Create a DashboardMongoDBDataSource instance with the specified connection name (mongoDataSourceConnection).

csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.ConnectionParameters;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {
    // ...
    DashboardMongoDBDataSource mongoDataSource = new DashboardMongoDBDataSource("MongoDB Data Source", "mongoDataSourceConnection");
}

Specify Connection Parameters

Handle the DashboardConfigurator.ConfigureDataConnection event to specify connection parameters. You can use a connection string or configure connection parameters manually.

Use a Connection String

  1. Create a MongoDBCustomConnectionParameters object with the specified connection string.
  2. Assign the created MongoDBCustomConnectionParameters object to the e.ConnectionParameters event parameter.
csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;

private void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
    if (e.ConnectionName == "mongoDataSourceConnection") {
        e.ConnectionParameters = new MongoDBCustomConnectionParameters("mongodb://mongodb0.example.com:27017");
    }
}

Configure Connection Parameters

  1. Create a MongoDBConnectionParameters object.
  2. Use Hostname and Port properties to specify the hostname and port.
  3. Optional. Enable the IsSRVRecord option if the hostname is an SRV record.
  4. Optional. Use the object’s AuthenticationInfo property to specify authentication credentials.
  5. Assign the created MongoDBCustomConnectionParameters object to the e.ConnectionParameters event parameter.
csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;

private void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
    if (e.ConnectionName == "mongoDataSourceConnection") {
        // MongoDB without authentication credentials.
        e.ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017);
    }
    if (e.ConnectionName == "mongoDataSourceConnectionWithAuth") {
        // MongoDB with authentication credentials.
        var infoScramSHA256 = new MongoDBScramSHA256AuthenticationInfo("UserName", "Password", "Northwind");
        e.ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017, infoScramSHA256);
    }
}

Load a Connection String from the Project’s Configuration File

See the following help topic for more information: ConnectionName.

Implement a Custom Connection Service

See the following help topic for more information: LoadConnection(String).

Create Data Queries

An instance of the MongoDBQuery class is a query to a MongoDB database collection. You can create multiple queries for different collections. To create a query, initialize a MongoDBQuery object and set its DatabaseName and CollectionName properties.

csharp
using DevExpress.DataAccess.MongoDB;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {
    // ...
    MongoDBQuery queryCategories = new MongoDBQuery() {
        DatabaseName = "Northwind",
        CollectionName = "Categories"
    };
    mongoDataSource.Queries.Add(queryCategories);

    MongoDBQuery queryProducts = new MongoDBQuery() {
        DatabaseName = "Northwind",
        CollectionName = "Products"
    };
    mongoDataSource.Queries.Add(queryProducts);
}

To filter collection items, set the query’s FilterString property. To apply conditional filters, add QueryParameter objects to the Parameters collection.

Each query name must be unique. The CollectionName property specifies the query name. To create multiple queries for the same collection, use the Alias property to specify different query names.

csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DataAccess;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {
    // ...
    MongoDBQuery queryProductsFiltered = new MongoDBQuery() {
        DatabaseName = "Northwind",
        CollectionName = "Products",
        FilterString = "[CategoryID] = ?CategoryID and [UnitPrice] > 30",
        Alias = "Filtered Products"
    };
    var queryParam = new DevExpress.DataAccess.MongoDB.QueryParameter("CategoryID", typeof(Expression), new Expression("?CategoryID", typeof(int)));
    queryProductsFiltered.Parameters.Add(queryParam);

    mongoDataSource.Queries.Add(queryProductsFiltered);
}

Register the MongoDB Data Source

Use the RegisterDataSource(String, XDocument) method to register the DataSourceInMemoryStorage object in the data source storage.

csharp
using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;

public static DataSourceInMemoryStorage CreateDataSourceStorage() {    
    // ...
    DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();        
    dataSourceStorage.RegisterDataSource("mongoDataSource", mongoDataSource.SaveToXml());

    return dataSourceStorage;
}

Use the Data Source Storage

Call the DashboardConfigurator.SetDataSourceStorage method and add the data source storage you configured. Handle the DashboardConfigurator.ConfigureDataConnection event. The following code uses the in-memory data source storage:

csharp
using System;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.MongoDB;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
    DashboardConfigurator configurator = new DashboardConfigurator();

    configurator.SetDataSourceStorage(CreateDataSourceStorage());
    configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;

    return configurator;
});

var app = builder.Build();

The MongoDB data source is now available in the Web Dashboard.

Users can bind dashboard items to data in the Web Dashboard’s UI.

Dashboard Data Source Wizard

Users can use the Dashboard Data Source Wizard to create a new MongoDB data source based on an existing connection.

See the following topic for details: Specify Data Source Settings (MongoDB).