Skip to main content
All docs
V24.1

Bind a Report to a MongoDB Instance (Runtime Sample)

  • 4 minutes to read

This topic describes how to bind a report to a MongoDB instance in code.

Watch Video: Bind a Report to the MongoDB Data Source (Tutorial)

View Example: Bind a Report to a MongoDB Instance

Note

The MongoDB.Driver package should be installed in your project to supply MongoDB data at runtime.

You should create and set up a MongoDBDataSource object to bind a report to a MongoDB instance in code. The sections below explain in detail how you can do it.

Specify Connection Parameters

Use one of the following techniques to specify connection parameters:

Use a Connection String

Create a MongoDBCustomConnectionParameters object and assign a connection string to the object’s ConnectionString property.

Configure Connection Fields Individually

  1. Create a MongoDBConnectionParameters object, and 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.

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. To filter items of the database collection, assign a filter condition to the query’s FilterString property.

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.

Create a MongoDB Data Source

Create a MongoDBDataSource object and assign the created connection parameters to the data source’s ConnectionParameters property. Add the data queries to the data source’s Queries collection. If you load a connection string from a project’s configuration file or implement a custom connection service, assign the name of the connection string to the data source’s ConnectionName property.

Use the MongoDB Data Source in Your Reporting Application

Assign the created MongoDBDataSource object to a report’s DataSource property. You can then bind report controls to data fields from this data source. Refer to the following topic for more information: Bind Report Controls to Data Using Expression Bindings.

Example

The example below demonstrates how to use the MongoDBDataSource class to bind a report to a MongoDB instance. The example uses the MongoDBCustomConnectionParameters class to specify a connection string to the MongoDB instance and the MongoDBQuery class to specify data queries to the Categories and Products collections of the Northwind database.

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
using DevExpress.XtraReports.UI;
// ...
// Create a MongoDBCustomConnectionParameters object and assign
// a connection string to a MongoDB instance to the object's
// ConnectionString property.
var connectionString = new MongoDBCustomConnectionParameters() {
    ConnectionString = "mongodb://localhost:27017"
};

// Specify data queries to the MongoDB instance.
var queryCategories = new MongoDBQuery() {
    DatabaseName = "Northwind",
    CollectionName = "Categories",
};

var queryProducts = new MongoDBQuery() {
    DatabaseName = "Northwind",
    CollectionName = "Products",
};

// Create a MongoDBDataSource object. Assign the created connection
// string to the object's ConnectionParameters property. Add the
// queries to the object's Queries collection.
var mongoDBDataSource = new MongoDBDataSource() {
    ConnectionParameters = connectionString,
    Queries = { queryCategories, queryProducts }
};

// Create a report. Set the report's DataSource property
// to the created mongoDBDataSource object.
var report = new XtraReport() {
    DataSource = mongoDBDataSource,
    DataMember = "Categories"
};

// Configure the report layout.
// ...