Skip to main content
All docs
V21.2

MongoDB Data Source

  • 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 following NuGet package in Visual Studio: MongoDB.Driver

Create a MongoDB Instance

Create a DashboardMongoDBDataSource instance with the specified connection name. Use this value to identify a data source when you configure the connection parameters.

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 the connection parameters to a MongoDB instance. You can use one of the ways below to configure them.

Use a Connection String

Create a MongoDBCustomConnectionParameters object and assign a connection string to the object’s ConnectionString property. Assign the created MongoDBCustomConnectionParameters object to the e.ConnectionParameters property.

using DevExpress.DataAccess.MongoDB;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.ConnectionParameters;

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

Configure Connection Fields Individually

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

private static void Default_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 a Project’s Configuration File

Refer to the ConnectionName property description for details.

Implement a Custom Connection Service

Refer to the LoadConnection(String) method description for more information.

Create Data Queries

An instance of the MongoDBQuery class is a query to a database collection of the MongoDB instance. You can create multiple queries to database collections. To create one query, initialize a MongoDBQuery object and assign the name of a database and the name of a database collection to the query’s DatabaseName and CollectionName properties.

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 database collection items, assign a filter condition to the query’s FilterString property. You can add QueryParameter to the Parameters to conditionally filter data loaded from a MongoDB instance.

The names of all queries to database collections should be unique. A string stored in a query’s CollectionName property is the default name for this query. To create several queries to the same database collection, use the Alias property to specify different names for these queries.

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 Instance

Add the MongoDB instance to the data source storage with the RegisterDataSource(String, XDocument) method call.

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:

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

public static class DashboardConfig {
    public static void RegisterService(RouteCollection routes) {   
        // ...
        DashboardConfigurator.Default.SetDashboardStorage(CreateDataSourceStorage());
        DashboardConfigurator.Default.ConfigureDataConnection += Default_ConfigureDataConnection;
    }
}

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

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