xtrareports-402975-feature-guide-to-devexpress-reports-bind-reports-to-data-mongodb-bind-a-report-to-mongodb-instance-runtime.md
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.
Use one of the following techniques to specify connection parameters:
Create a MongoDBCustomConnectionParameters object and assign a connection string to the object’s ConnectionString property.
Refer to the ConnectionName property description for details.
Refer to the LoadConnection(String) method description for more information.
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 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.
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.
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.
// ...
' INSTANT VB NOTE: This code snippet uses implicit typing. You will need to set 'Option Infer On' in the VB file or set 'Option Infer' at the project level.
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.MongoDB
Imports DevExpress.XtraReports.UI
' ...
' Create a MongoDBCustomConnectionParameters object and assign
' a connection string to a MongoDB instance to the object's
' ConnectionString property.
Private connectionString = New MongoDBCustomConnectionParameters() With {.ConnectionString = "mongodb://localhost:27017"}
' Specify data queries to the MongoDB instance.
Private queryCategories = New MongoDBQuery() With {.DatabaseName = "Northwind", .CollectionName = "Categories"}
Private queryProducts = New MongoDBQuery() With {.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.
Private mongoDBDataSource = New MongoDBDataSource() With {
.ConnectionParameters = connectionString, .Queries = { queryCategories, queryProducts }
}
' Create a report. Set the report's DataSource property
' to the created mongoDBDataSource object.
Private report = New XtraReport() With {.DataSource = mongoDBDataSource, .DataMember = "Categories"}
' Configure the report layout.
' ...