Bind a Dashboard to MongoDB
- 3 minutes to read
The WinForms Designer allows you to connect to MongoDB in the Data Source Wizard or in code.
Note
The MongoDB.Driver package should be installed in your project to supply MongoDB data at runtime and display MongoDB in the Data Source Wizard.
Create a Data Source in the Data Source Wizard
Follow the steps below to establish a connection to a database:
Click the New Data Source button in the Data Source ribbon tab.
On the first page of the invoked Data Source Wizard dialog, select MongoDB and click Next.
Specify connection parameters on the next page. You can pass an entire string or enter connection fields individually. Refer to the following topic for information about connection string format and options: Connection String URI Format.
The following page allows you to configure queries. Select databases and collections that you want to load from the MongoDB instance. A string stored in a query’s Collection column is the default name for the query. The names of MongoDB queries should be unique. You can use the Alias column to set unique names for queries in the same collection. To filter queries, add a filter string to the Filter column.
Create a Data Source in Code
To connect to MongoDB you need to create a DashboardMongoDBDataSource instance, configure connection parameters and specify data queries. Refer to the sections below for details.
Configure Connection Parameters
Use one of the following ways:
Create a MongoDBCustomConnectionParameters object and assign a connection string to the MongoDBCustomConnectionParameters.ConnectionString property.
Assign a MongoDBConnectionParameters instance with specified connection parameters to the MongoDBDataSourceBase.ConnectionParameters property.
You can specify the following connection parameters:
- Hostname
- Assign a database hostname to the MongoDBConnectionParameters.Hostname property.
- Port
- Assign a database port to the MongoDBConnectionParameters.Port property.
- SRVRecord
- If the hostname contains an SRV record, set the MongoDBConnectionParameters.IsSRVRecord property to true.
- Authentication
- Use the MongoDBConnectionParameters.AuthenticationInfo property to specify authentication credentials.
Specify Data Queries
Initialize a MongoDBQuery object and assign the database and collection names to the MongoDBQuery.DatabaseName and MongoDBQuery.CollectionName properties. Use the MongoDBQuery.FilterString property to specify a filter string for data loaded from the MongoDB instance.
A string stored in the CollectionName property is the default name for the query. The names of MongoDB queries should be unique. Use the Alias property to set unique names for queries.
Add the created queries to the Queries collection.
Create a MongoDB Data Source
Create a DashboardMongoDBDataSource
object and add it to the Dashboard.DataSources collection.
The following code snippet demonstrates how to bind a dashboard to a DashboardMongoDBDataSource
instance:
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
//...
var dashboard = new Dashboard();
var dataSource = new DashboardMongoDBDataSource() {
ConnectionParameters = new MongoDBConnectionParameters("localhost", false, 27017)
};
var queryCategories = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Categories"
};
dataSource.Queries.Add(queryCategories);
var queryProducts = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Products"
};
dataSource.Queries.Add(queryProducts);
dashboard.DataSources.Add(dataSource);